#import "ViewController.h"
@interface ViewController ()
// 这个layer层是渐变色的层
@property (nonatomic, strong) CAGradientLayer *gradientLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self testSomeThings];
}
- (void)testSomeThings
{
// 初始化渐变层
self.gradientLayer = [CAGradientLayer layer];
self.gradientLayer.frame = self.view.bounds;
// 设置渐变层的颜色
CGColorRef color1 = [UIColor colorWithWhite:0.5 alpha:0.2].CGColor;
CGColorRef color2 = [UIColor colorWithRed:1.0 green:0 blue:0 alpha:0.4].CGColor;
CGColorRef color3 = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.3].CGColor;
CGColorRef color4 = [UIColor colorWithWhite:0.4 alpha:0.2].CGColor;
self.gradientLayer.colors = [NSArray arrayWithObjects:(__bridge id _Nonnull)(color1), color2, color3, color4, nil];
self.gradientLayer.locations = @[@0.10, @0.30, @0.50, @0.70, @0.90];
self.gradientLayer.startPoint = CGPointMake(0, 0); // 范围是0-1
self.gradientLayer.endPoint = CGPointMake(1, 1);
[self.view.layer addSublayer:self.gradientLayer];
[self shapeLayer];
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(randomColor:) userInfo:nil repeats:YES];
}
- (void)randomColor:(NSTimer *)sender
{
CGFloat redValue = drand48();
CGFloat blueValue = drand48();
CGFloat greenValue = drand48();
self.view.backgroundColor = [UIColor colorWithRed:redValue green:greenValue blue:blueValue alpha:1.0];
}
- (void)shapeLayer
{
// 画图的layer层
CAShapeLayer *layer = [CAShapeLayer layer];
// 创建路径
CGMutablePathRef path = CGPathCreateMutable();
// 设置起点(path:给哪个路径设置起点)
CGPathMoveToPoint(path, NULL, 0, 0);
// 加载一条线到某个点
CGPathAddLineToPoint(path, NULL, self.view.bounds.size.width, self.view.bounds.size.height);
layer.path = path;
layer.fillColor = [UIColor whiteColor].CGColor;
layer.strokeColor = [UIColor redColor].CGColor;
layer.strokeEnd = 1;
[self.view.layer addSublayer:layer];
// 给layer层添加动画
CABasicAnimation *anim = [CABasicAnimation animation];
anim.keyPath = @"strokeEnd";
anim.fromValue = @0;
anim.toValue = @1;
anim.duration = 5;
[layer addAnimation:anim forKey:nil];
[self.view setNeedsDisplay];
}
layer层
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 学习Swift也有了一段时间,但由于身体原因,中间来了个小插曲,让我对Swift的学习进度慢了下来,但却不曾削弱我...
- Copyright © 2017年ZaneWangWang. All rights reserved. CAA...
- 这篇文章主要对layer层动画CAAnimation进行一个详解.CAAnimation是一个抽象类,我们不直接使...
- Copyright © 2017年ZaneWangWang. All rights reserved. 如...