//为颜色设置渐变效果
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
CAGradientLayer *gradient = [CAGradientLayer layer];
//设置开始和结束位置(设置渐变的方向)
gradient.startPoint = CGPointMake(0, 0);
gradient.endPoint = CGPointMake(1, 0);
gradient.frame =CGRectMake(0,0,40,40);
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor redColor].CGColor,(id)[UIColorwhiteColor].CGColor,nil];
[view.layer insertSublayer:gradient atIndex:0];
[self.view addSubview:view];
//为透明度设置渐变效果
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
UIColor *colorOne = [UIColor colorWithRed:(216/255.0) green:(0/255.0) blue:(18/255.0) alpha:1.0];
UIColor *colorTwo = [UIColor colorWithRed:(216/255.0) green:(0/255.0) blue:(18/255.0) alpha:0.0];
NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];
CAGradientLayer *gradient = [CAGradientLayer layer];
//设置开始和结束位置(设置渐变的方向)
gradient.startPoint = CGPointMake(0, 0);
gradient.endPoint = CGPointMake(1, 0);
gradient.colors = colors;
gradient.frame = CGRectMake(0, 0, 40, 40);
[view.layerinsertSublayer:gradientatIndex:0];
[self.view addSubview:view];