乔帮主的遗产

iOS开发日记-为UIView添加渐变色

2018-05-10  本文已影响178人  Mr_Ten

在日常开发中,常规的颜色已经无法满足美工的需求了,为了满足需求,下面写一下用简单的渐变色添加,UIView以及UIView的子类都可以按照以下步骤应用:

1.给颜色设置渐变效果

//为颜色设置渐变效果:
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];

2.为透明度设置渐变效果

//为透明度设置渐变效果
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];
上一篇下一篇

猜你喜欢

热点阅读