当我们选择它时,我们是否可以更改uibarbuttonitem的颜色/色调并突出显示?我正在创建的应用程序将经常使用外部门,并希望在高眩光情况下它更明显,让用户知道他实际上按下了按钮。uibarbuttonitem高亮色调/颜色
编辑:我想改变颜色的按钮的高亮显示状态
当我们选择它时,我们是否可以更改uibarbuttonitem的颜色/色调并突出显示?我正在创建的应用程序将经常使用外部门,并希望在高眩光情况下它更明显,让用户知道他实际上按下了按钮。uibarbuttonitem高亮色调/颜色
编辑:我想改变颜色的按钮的高亮显示状态
我没有试过,但你可以尝试继承UIBarButtonItem
,然后重写touchesEnded:withEvent:
和touchesBegan:withEvent:
方法,然后使用它们来设置tintColor
为您的UIBarButtonItem
实例。
你会添加到您的UIBarButtonItem
子类:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.tintColor = [UIColor redColor];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
self.tintColor = [UIColor blueColor];
}
一个UIButton是UIControl的子类,它有一个adjustsImageWhenHighlighted,一个showsTouchWhenHighlighted和showsTouchWhenHighlighted财产。
UIBarButtonItem是UIBarItem的子类,没有这些。它确实有一个UIBarButtonItemStyle,当它被设置为UIBarButtonItemStylePlain时,它表示点击时按钮发光(但不能指定颜色)。
您可以继承UIBarButtonItem并将UIButton放入其中,并使用不同背景图像颜色的不同状态。
- (id)initWithImage:(UIImage *)image target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = (CGRect){CGPointZero, image.size};
[button setBackgroundImage:image forState:UIControlStateNormal];
UIImage *highlightedImage = [image imageWithColor:[UIColor textHighlightColor]];
[button setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
[button setBackgroundImage:highlightedImage forState:UIControlStateSelected];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
self = [self initWithCustomView:button];
return self;
}
你需要把这个变成UIImage的类别:
- (UIImage *)imageWithColor:(UIColor *)color
{
// begin a new image context, to draw our colored image onto
UIGraphicsBeginImageContext(self.size);
// get a reference to that context we created
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
// translate/flip the graphics context (for transforming from CG* coords to UI* coords
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// set the blend mode to color burn, and the original image
CGContextSetBlendMode(context, kCGBlendModeMultiply);
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextDrawImage(context, rect, self.CGImage);
// set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
CGContextClipToMask(context, rect, self.CGImage);
CGContextAddRect(context, rect);
CGContextDrawPath(context,kCGPathFill);
// generate a new UIImage from the graphics context we drew onto
UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//return the color-burned image
return coloredImg;
}
可能的,但是当用户抬起手指离开屏幕,从而恢复到原来的颜色不会touchesEnded结束了吗?在那里我正在寻找保持新的颜色,直到代码执行。这可以起到退回的作用,至少可以更好地表明用户确实按下了按钮。 – Padin215
...好的,在这种情况下,只需从我的示例中省略'touchesEnded:withEvent:'...? –
我想在方法结束时,我可以重置tintColor到正常...我会试一试,谢谢。 – Padin215