我需要复制UIButton在图像上点击时产生的高亮效果。如图:
原始PNG是一个带有alpha背景的正方形。当我将其设置为UIButton的图像时,自动应用于图像的非alpha像素的效果。
如何实现这种效果?
我需要复制UIButton在图像上点击时产生的高亮效果。如图:
原始PNG是一个带有alpha背景的正方形。当我将其设置为UIButton的图像时,自动应用于图像的非alpha像素的效果。
如何实现这种效果?
你可以使用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值)。
self.scale
会更好。 - omz