<pre>
- (void)viewDidLoad {
[super viewDidLoad];
CAShapeLayer *circleLayer = [self createCirleView];
CAShapeLayer *squareLayer = [self createSquareView];
CAShapeLayer *roundSquareLayer = [self createRoundSquareView];
[self addAnimationToLayer:circleLayer];
[self addAnimationToLayer:squareLayer];
[self addAnimationToLayer:roundSquareLayer];
}
pragma mark - ++++++++++++++++++++++++++ 贝塞尔曲线(创建一个圆) ++++++++++++++++++++++++++
-(CAShapeLayer *)createCirleView{
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100 / 2.f, 100 / 2.f)
radius:100 / 2.f
startAngle:0
endAngle:M_PI
clockwise:NO];
// 创建一个shapeLayer
CAShapeLayer *layer = [CAShapeLayer layer];
layer.frame = CGRectMake(100, 50, 100, 100);
layer.strokeColor = [UIColor greenColor].CGColor; // 边缘线的颜色
layer.fillColor = [UIColor clearColor].CGColor; // 闭环填充的颜色
layer.lineCap = kCALineCapRound; // 线端点的类型
layer.path = path.CGPath; // 从贝塞尔曲线获取到形状
layer.lineWidth = 20.0f; // 线条宽度
layer.strokeStart = 0.0f; //画笔开始位置
layer.strokeEnd = 1.0f; //画笔结束位置
// 将layer添加进图层
[self.view.layer addSublayer:layer];
return layer;
}
pragma mark - ++++++++++++++++++++++++++ 贝塞尔曲线(创建一个长方形) ++++++++++++++++++++++++++
-(CAShapeLayer *)createSquareView{
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 150, 150)];
// 创建一个shapeLayer
CAShapeLayer *layer = [CAShapeLayer layer];
layer.frame = CGRectMake(100, 200, 100, 100);
layer.strokeColor = [UIColor greenColor].CGColor; // 边缘线的颜色
layer.fillColor = [UIColor clearColor].CGColor; // 闭环填充的颜色
layer.lineCap = kCALineCapRound; // 线端点的类型
layer.path = path.CGPath; // 从贝塞尔曲线获取到形状
layer.lineWidth = 20.0f; // 线条宽度
layer.strokeStart = 0.0f; //画笔开始位置
layer.strokeEnd = 1.0f; //画笔结束位置
// 将layer添加进图层
[self.view.layer addSublayer:layer];
return layer;
}
pragma mark - ++++++++++++++++++++++++++ 贝塞尔曲线(创建一个圆角长方形) ++++++++++++++++++++++++
-(CAShapeLayer *)createRoundSquareView{
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 150, 150) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(30, 30)];
// 创建一个shapeLayer
CAShapeLayer *layer = [CAShapeLayer layer];
layer.frame = CGRectMake(100, 400, 100, 100);
layer.strokeColor = [UIColor greenColor].CGColor; // 边缘线的颜色
layer.fillColor = [UIColor clearColor].CGColor; // 闭环填充的颜色
layer.lineCap = kCALineCapRound; // 线端点的类型
layer.path = path.CGPath; // 从贝塞尔曲线获取到形状
layer.lineWidth = 20.0f; // 线条宽度
layer.strokeStart =
![Uploading 屏幕快照 2016-11-18 16.29.14_854524.png . . .] 0.0f; //画笔开始位置
layer.strokeEnd = 1.0f; //画笔结束位置
// 将layer添加进图层
[self.view.layer addSublayer:layer];
return layer;
}
-(void)addAnimationToLayer:(CAShapeLayer *)layer{
layer.speed = 0.5;
layer.strokeStart = 0.0f;
layer.strokeEnd = 1.0f;
layer.lineWidth = 4.0f;
// 给这个layer添加动画效果
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 1.0;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[layer addAnimation:pathAnimation forKey:nil];
}
</pre>