How to implement highlighting on UIImage like UIButton does when tapped?(如何在 UIImage 上像 UIButton 一样在点击时实现突出显示?)
问题描述
我需要复制 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).
这篇关于如何在 UIImage 上像 UIButton 一样在点击时实现突出显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 UIImage 上像 UIButton 一样在点击时实现突出显示?


基础教程推荐
- NSString intValue 不能用于检索电话号码 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01