<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 - ++++++++++++++++++++++++++ 貝塞爾曲線(創(chuàng)建一個圓) ++++++++++++++++++++++++++
-(CAShapeLayer *)createCirleView{
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100 / 2.f, 100 / 2.f)
radius:100 / 2.f
startAngle:0
endAngle:M_PI
clockwise:NO];
// 創(chuàng)建一個shapeLayer
CAShapeLayer *layer = [CAShapeLayer layer];
layer.frame = CGRectMake(100, 50, 100, 100);
layer.strokeColor = [UIColor greenColor].CGColor; // 邊緣線的顏色
layer.fillColor = [UIColor clearColor].CGColor; // 閉環(huán)填充的顏色
layer.lineCap = kCALineCapRound; // 線端點的類型
layer.path = path.CGPath; // 從貝塞爾曲線獲取到形狀
layer.lineWidth = 20.0f; // 線條寬度
layer.strokeStart = 0.0f; //畫筆開始位置
layer.strokeEnd = 1.0f; //畫筆結(jié)束位置
// 將layer添加進(jìn)圖層
[self.view.layer addSublayer:layer];
return layer;
}
pragma mark - ++++++++++++++++++++++++++ 貝塞爾曲線(創(chuàng)建一個長方形) ++++++++++++++++++++++++++
-(CAShapeLayer *)createSquareView{
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 150, 150)];
// 創(chuàng)建一個shapeLayer
CAShapeLayer *layer = [CAShapeLayer layer];
layer.frame = CGRectMake(100, 200, 100, 100);
layer.strokeColor = [UIColor greenColor].CGColor; // 邊緣線的顏色
layer.fillColor = [UIColor clearColor].CGColor; // 閉環(huán)填充的顏色
layer.lineCap = kCALineCapRound; // 線端點的類型
layer.path = path.CGPath; // 從貝塞爾曲線獲取到形狀
layer.lineWidth = 20.0f; // 線條寬度
layer.strokeStart = 0.0f; //畫筆開始位置
layer.strokeEnd = 1.0f; //畫筆結(jié)束位置
// 將layer添加進(jìn)圖層
[self.view.layer addSublayer:layer];
return layer;
}
pragma mark - ++++++++++++++++++++++++++ 貝塞爾曲線(創(chuàng)建一個圓角長方形) ++++++++++++++++++++++++
-(CAShapeLayer *)createRoundSquareView{
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 150, 150) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(30, 30)];
// 創(chuàng)建一個shapeLayer
CAShapeLayer *layer = [CAShapeLayer layer];
layer.frame = CGRectMake(100, 400, 100, 100);
layer.strokeColor = [UIColor greenColor].CGColor; // 邊緣線的顏色
layer.fillColor = [UIColor clearColor].CGColor; // 閉環(huán)填充的顏色
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; //畫筆結(jié)束位置
// 將layer添加進(jìn)圖層
[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>
