如果说CAShapeLayer是用于提供设置形状的,那么CAGradientLayer是用于提供设置颜色的
英语单词:Gradient:
CAGradientLayer简介
使用CAGradientLayer主要的方法
下面展示几个关于CAGradientLayer的效果图。
最后给出YouXianMing老师的四个关于CAGradientLayer的Example Code下载链接: http://pan.baidu.com/s/1eR0Q7Hw 密码: 7zvm
用于体会的源码
#import "CAGradientLayerController.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@interface CAGradientLayerController ()
/** GradientLayer */
@property (nonatomic,weak)CAGradientLayer *gradientLayer;
@property (nonatomic,weak)CAGradientLayer *gradientLayer2;
@end
@implementation CAGradientLayerController
- (void)viewDidLoad {
[super viewDidLoad];
// 第一个layer
NSArray* colors = @[(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor blueColor].CGColor];
self.gradientLayer = [self addGradientLayerWithFrame:CGRectMake(ScreenWidth * 0.5 - 100, 50, 200, 100) colors:colors locations:nil];
// 第二个layer
NSArray* colors2 = @[(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor blueColor].CGColor];
NSArray* locations2 = @[@(0.5),@(0.75)];
self.gradientLayer = [self addGradientLayerWithFrame:CGRectMake(ScreenWidth * 0.5 - 100, 170, 200, 100) colors:colors2 locations:locations2];
// 第三个layer
NSArray* colors3 = @[(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor greenColor].CGColor,
(__bridge id)[UIColor blueColor].CGColor];
NSArray* locations3 = @[@(0),@(0.5)];
self.gradientLayer = [self addGradientLayerWithFrame:CGRectMake(ScreenWidth * 0.5 - 100, 290, 200, 100) colors:colors3 locations:locations3];
// 第四个layer
NSArray* colors4 = @[(__bridge id)[UIColor blackColor].CGColor,//黑色
(__bridge id)[UIColor whiteColor].CGColor,//白色
(__bridge id)[UIColor greenColor].CGColor,//绿色
(__bridge id)[UIColor blueColor].CGColor, //蓝色
(__bridge id)[UIColor grayColor].CGColor, //灰色
(__bridge id)[UIColor brownColor].CGColor //棕色
];
NSArray* locations4 = @[@(0.25),@(0.5),@(0.75),@(0.85)];
/*
* 0~0.25 黑色◎
* 0.25~0.5 黑色↓ 白色↑
* 0.5~0.75 白色↓ 绿色↑
* 0.75~0.85 绿色↓ 蓝色↑
* 0.85~1.0 蓝色↓ 灰色↑
* 因为到1.0就结束了,所以棕色就是多余的了
*/
self.gradientLayer = [self addGradientLayerWithFrame:CGRectMake( 2, 410, ScreenWidth - 4, 100) colors:colors4 locations:locations4];
}
-(CAGradientLayer*)addGradientLayerWithFrame:(CGRect)frame colors:(NSArray<NSNumber*>*)colors locations:(NSArray<NSNumber*>*)locations {
CAGradientLayer* gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = frame;
gradientLayer.colors = colors;
gradientLayer.locations = locations;
gradientLayer.borderWidth = 1.0f;
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(1, 0);
[self.view.layer addSublayer:gradientLayer];
return gradientLayer;
}
@end
CAGradientLayer的颜色分割点是以0到1的比例来计算的
补充:
注意:
#import "CAGradientLayerController.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@interface CAGradientLayerController ()
/** GradientLayer */
@property (nonatomic,weak)CAGradientLayer *gradientLayer;
/** Timer */
@property (nonatomic,strong)NSTimer *timer;
@end
@implementation CAGradientLayerController
- (void)viewDidLoad {
[super viewDidLoad];
// 添加背景
UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg"]];
imageView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
[self.view addSubview:imageView];
// 添加色差层
NSArray* colors = @[(__bridge id)[UIColor clearColor].CGColor,
(__bridge id)[UIColor clearColor].CGColor];
NSArray* locations = @[@(0)];
self.gradientLayer = [self addGradientLayerWithFrame:imageView.frame colors:colors locations:locations];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(changeGradientColor)
userInfo:nil
repeats:YES];
}
-(void)changeGradientColor{
self.gradientLayer.colors = @[(__bridge id)[UIColor clearColor].CGColor,
(__bridge id)[UIColor colorWithRed:arc4random() % 255 / 255.0f
green:arc4random() % 255 / 255.0f
blue:arc4random() % 255 / 255.0f
alpha:1].CGColor];
//self.gradientLayer.locations = @[@(arc4random() % 10 / 10.0f)];
//这个效果不好,效果是直接铺盖上去的,但是下面一种情况,是形变也是有过程的。
self.gradientLayer.locations = @[@(arc4random() % 10 / 10.0f),@(1.0f)];
}
-(CAGradientLayer*)addGradientLayerWithFrame:(CGRect)frame colors:(NSArray<NSNumber*>*)colors locations:(NSArray<NSNumber*>*)locations {
CAGradientLayer* gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = frame;
gradientLayer.colors = colors;
gradientLayer.locations = locations;
gradientLayer.borderWidth = 1.0f;
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(0, 1);
[self.view.layer addSublayer:gradientLayer];
return gradientLayer;
}
@end
重写setter方法做动画
对外的需求:
效果图:
源码在github上:https://github.com/HeYang123456789/UIView
原文:http://www.cnblogs.com/goodboy-heyang/p/5185595.html