问题描述
我需要在点按时突出显示UIButton在图像上的效果.参见:
I need to replicate the effect that the UIButton does on an image when tapped, the highlighting. See:
原始PNG是带有alpha背景的正方形.当我将其设置为UIButton的图像时,它会自动在图像的非alpha像素上应用效果.
The original PNG is a square with alpha background. When I set it as UIButton's image it automatically apply an effect on the non-alpha pixels of the image.
如何实现这种效果?
推荐答案
您可以在UIImage上使用简单的类别来实现此目标:
You could achieve this with a simple category on UIImage:
@interface UIImage (Tint)
- (UIImage *)tintedImageUsingColor:(UIColor *)tintColor;
@end
@implementation UIImage (Tint)
- (UIImage *)tintedImageUsingColor:(UIColor *)tintColor {
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
CGRect drawRect = CGRectMake(0, 0, self.size.width, self.size.height);
[self drawInRect:drawRect];
[tintColor set];
UIRectFillUsingBlendMode(drawRect, kCGBlendModeSourceAtop);
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
@end
对于上面显示的效果,您将传递[UIColor colorWithWhite:0.0 alpha:0.3]
之类的内容作为tintColor参数(带有alpha值的实验).
For the effect shown above, you'd pass something like [UIColor colorWithWhite:0.0 alpha:0.3]
as the tintColor parameter (experiment with the alpha value).
这篇关于点击时如何像UIButton一样在UIImage上实现突出显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!