方法一.使用CAGradientLayer
//初始化顏色漸變圖層和圖層范圍
CAGradientLayer *layer =? [CAGradientLayer new];
layer.frame = CGRectMake(100,100, 100, 100);
//初始化屬性 colors數(shù)組 設(shè)置漸變的顏色 ? //當(dāng)前添加四種顏色
layer.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] colorWithAlphaComponent:1].CGColor,(id)[[UIColor yellowColor] colorWithAlphaComponent:1].CGColor,(id)[[UIColor blueColor] colorWithAlphaComponent:1].CGColor,(id)[UIColor clearColor].CGColor, nil];
//初始化屬性locations,漸變顏色的區(qū)間分布贱除,locations的數(shù)組長度和color一致旬昭,這個(gè)值一般不用管它权纤,默認(rèn)是nil,會(huì)平均分布邑狸。
layer.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.3],[NSNumber numberWithFloat:0.8],[NSNumber numberWithFloat:1.0], nil];
[self.view.layer addSublayer:layer];
//補(bǔ)充屬性說明:?
@property (nonatomic,assign) CGPoint startPoint; ?//映射locations中第一個(gè)位置坪郭,用單位向量表示赊颠,比如(0圾结,0)表示從左上角開始變化瑰剃。默認(rèn)值是(0.5,0.0)。
@property (nonatomic,assign) CGPoint endPoint; ?//映射locations中最后一個(gè)位置筝野,用單位向量表示晌姚,比如(1,1)表示到右下角變化結(jié)束歇竟。默認(rèn)值是(0.5,1.0)挥唠。
@property (nonatomic,assign) NSString *type; ? //默認(rèn)值是kCAGradientLayerAxial,表示按像素均勻變化途蒋。除了默認(rèn)值也無其它選項(xiàng)。
其中l(wèi)ocations startPoint和endPoint對(duì)應(yīng)的坐標(biāo)馋记,如下:
運(yùn)行效果:(適合矩形的顏色漸變)
方法二:通過繪畫漸變上下文實(shí)現(xiàn)? CGContextDrawLinearGradient
//底部上下漸變效果背景
CGRect frame = self.view.frame;
UIImageView *imgview = [[UIImageView alloc]initWithFrame:frame];
[self.view addSubview:imgview];
// The following methods will only return a 8-bit per channel context in the DeviceRGB color space. 通過圖片上下文設(shè)置顏色空間間
UIGraphicsBeginImageContext(imgview.frame.size);
//獲得當(dāng)前的上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//創(chuàng)建顏色空間 /* Create a DeviceRGB color space. */
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
//通過矩陣調(diào)整空間變換
CGContextScaleCTM(context, frame.size.width, frame.size.height);
//設(shè)置顏色 矩陣
CGFloat colors[] = {
253.0/255.0, 163.0/255.0, 87.0/255.0, 1.0,
253.0/255.0, 163.0/255.0, 87.0/255.0, 0.0,
};
//通過顏色組件獲得漸變上下文
CGGradientRef backGradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
//釋放顏色漸變
CGColorSpaceRelease(rgb);
//通過上下文繪畫線色漸變
CGContextDrawLinearGradient(context, backGradient, CGPointMake(0.5, 0), CGPointMake(0.5, 1), kCGGradientDrawsBeforeStartLocation);
//通過圖片上下文獲得照片
imgview.image = UIGraphicsGetImageFromCurrentImageContext();
運(yùn)行效果:
此方法還可以創(chuàng)建從一個(gè)點(diǎn)向周圍 漸變的 圓形漸變效果效果
//發(fā)散球效果
UIImageView *clearCircleImgView = [[UIImageView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:clearCircleImgView];
UIGraphicsBeginImageContext(clearCircleImgView.frame.size);
CGContextRef clearCircleContext = UIGraphicsGetCurrentContext();
CGColorSpaceRef clearCircleRGB = CGColorSpaceCreateDeviceRGB();
//矩陣 設(shè)置顏色的漸變透明度和漸變球的半徑大泻牌隆(矩陣的行數(shù))
CGFloat clearCircleColors[] = {
249.0/255.0, 204.0/255.0, 23.0/255.0, 0.5,
249.0/255.0, 204.0/255.0, 23.0/255.0, 0.35,
249.0/255.0, 204.0/255.0, 23.0/255.0, 0.2,
249.0/255.0, 204.0/255.0, 23.0/255.0, 0.05,
};?
CGGradientRef clearCircleGradient = CGGradientCreateWithColorComponents(clearCircleRGB, clearCircleColors, NULL, sizeof(clearCircleColors[0]*4));
CGContextDrawRadialGradient(clearCircleContext, clearCircleGradient, CGPointMake(160, 160),20, CGPointMake(160, 160),250, kCGGradientDrawsBeforeStartLocation);
clearCircleImgView.image = UIGraphicsGetImageFromCurrentImageContext();
CGGradientRelease(clearCircleGradient);
運(yùn)行效果: