在ios开发的过程中,我们经常会遇到这样的情况:当我们点击一个按钮时,按钮的背景颜色会发生变化,一般情况下 我们会调用UIButton 的函数- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state 来设置button在正常的情况下和按下时的背景颜色。 此时我们就需要两张图片,制作图片的过程很复杂。
因此我们可以通过修改图片的背景颜色达到这样的效果:
blendMode在ios中是一个枚举类型:如下所示
enum CGBlendMode {
kCGBlendModeNormal,
kCGBlendModeMultiply,
kCGBlendModeScreen,
kCGBlendModeOverlay,
kCGBlendModeDarken,
kCGBlendModeLighten,
kCGBlendModeColorDodge,
kCGBlendModeColorBurn,
kCGBlendModeSoftLight,
kCGBlendModeHardLight,
kCGBlendModeDifference,
kCGBlendModeExclusion,
kCGBlendModeHue,
kCGBlendModeSaturation,
kCGBlendModeColor,
kCGBlendModeLuminosity,
/* Available in Mac OS X 10.5 & later. R, S, and D are, respectively,
premultiplied result, source, and destination colors with alpha; Ra,
Sa, and Da are the alpha components of these colors.
The Porter-Duff "source over" mode is called `kCGBlendModeNormal':
R = S + D*(1 - Sa)
Note that the Porter-Duff "XOR" mode is only titularly related to the
classical bitmap XOR operation (which is unsupported by
CoreGraphics). */
kCGBlendModeClear, /* R = 0 */
kCGBlendModeCopy, /* R = S */
kCGBlendModeSourceIn, /* R = S*Da */
kCGBlendModeSourceOut, /* R = S*(1 - Da) */
kCGBlendModeSourceAtop, /* R = S*Da + D*(1 - Sa) */
kCGBlendModeDestinationOver, /* R = S*(1 - Da) + D */
kCGBlendModeDestinationIn, /* R = D*Sa */
kCGBlendModeDestinationOut, /* R = D*(1 - Sa) */
kCGBlendModeDestinationAtop, /* R = S*(1 - Da) + D*Sa */
kCGBlendModeXOR, /* R = S*(1 - Da) + D*(1 - Sa) */
kCGBlendModePlusDarker, /* R = MAX(0, (1 - D) + (1 - S)) */
kCGBlendModePlusLighter /* R = MIN(1, S + D) */
};
注:premultiplied result, source, and destination colors with alpha; Ra,
Sa, and Da are the alpha components of these colors. R:表示结果 S表示包含alpha的原色, D表示含有alpha的目标色,Ra,Sa,Da分别是3个的alpja
(一) 首先创建一个UIImage的类别:
@interface UIImage (Tint)
-(UIImage *)imageWithTintColor:(UIColor *)tintColor;
-(UIImage *) imageWithGradientTintClolor:(UIColor *)tintColor;
@end
(二) 实现类别中的方法
//
// UIImage + Tint.m
// practice
//
// Created by user on 15/3/24.
// Copyright (c) 2015年 user. All rights reserved.
//
#import "UIImage + Tint.h"
@implementation UIImage (Tint)
-(UIImage *)imageWithTintColor:(UIColor *)tintColor blendMode: (CGBlendMode) blendMode
{
UIGraphicsBeginImageContextWithOptions(self.size, NO,0.0f);
[tintColor setFill];
CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
UIRectFill(bounds);
[self drawInRect:bounds blendMode:blendMode alpha:1.0f];
if(blendMode != kCGBlendModeDestinationIn)
{
[self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
-(UIImage *)imageWithTintColor:(UIColor *)tintColor
{
return [self imageWithTintColor:tintColor blendMode:kCGBlendModeDestinationIn];// 保留原图的透明度
}
-(UIImage *) imageWithGradientTintClolor:(UIColor *)tintColor
{
return [self imageWithTintColor:tintColor blendMode:kCGBlendModeOverlay]; //保持背景色的明暗,就是灰暗信息
}
@end
(三)使用类别中的方法
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(150, 150, 34, 34)];
UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(200, 200, 34, 34)];
UIImageView *imageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(250,250, 34, 34)];
imageView1.image = [UIImage imageNamed:@"auditing_btn_message_pre@2x.png"];
imageView2.image = [[UIImage imageNamed:@"auditing_btn_message_pre@2x.png"] imageWithTintColor:[UIColor orangeColor]];
imageView3.image = [[UIImage imageNamed:@"auditing_btn_message_pre@2x.png"] imageWithGradientTintClolor:[UIColor orangeColor]];
[_firstView addSubview:imageView1];
[_firstView addSubview:imageView2];
[_firstView addSubview:imageView3];
程序运行之后的效果
分别是3张图片的放在视图上显示的结果