@interface ViewController ()<CAAnimationDelegate>
{
CAShapeLayer *maskLayer;
CABasicAnimation *_animation;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
{
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:80 startAngle:0 endAngle:M_PI clockwise:NO];
maskLayer = [CAShapeLayer layer];
maskLayer.backgroundColor = [UIColor clearColor].CGColor;
maskLayer.path = bezierPath.CGPath;
maskLayer.strokeColor = [UIColor redColor].CGColor;
maskLayer.fillColor = [UIColor whiteColor].CGColor;
maskLayer.lineWidth = 20;
// maskLayer.fillRule = kCAFillRuleEvenOdd;
maskLayer.lineCap = kCALineCapRound;
[self.view.layer addSublayer:maskLayer];
maskLayer.strokeStart = 0;
maskLayer.strokeEnd = 1;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.fromValue = @0;
animation.duration = 3.0f;
animation.delegate = self;
[animation setValue:@"BasicAnimationEnd" forKey:@"animationName"];
[maskLayer addAnimation:animation forKey:@"BasicAnimationEnd"];
}
}
- (void)animationDidStart:(CAAnimation *)anim{
if (_animation) {
UIBezierPath *bezierPath1 = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:30 startAngle:M_PI endAngle:0 clockwise:NO];
maskLayer = [CAShapeLayer layer];
maskLayer.backgroundColor = [UIColor clearColor].CGColor;
maskLayer.path = bezierPath1.CGPath;
maskLayer.strokeColor = [UIColor redColor].CGColor;
maskLayer.fillColor = [UIColor whiteColor].CGColor;
maskLayer.lineWidth = 15;
maskLayer.fillRule = kCAFillRuleEvenOdd;
maskLayer.lineCap = kCALineCapRound;
[self.view.layer addSublayer:maskLayer];
maskLayer.strokeStart = 0;
maskLayer.strokeEnd = 1;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.fromValue = @0;
animation.duration = 0.8f;
animation.repeatCount = 0;
[animation setValue:@"BasicAnimationEnd" forKey:@"animationName"];
[maskLayer addAnimation:animation forKey:@"BasicAnimationEnd"];
}else{
UIBezierPath *bezierPath1 = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:60 startAngle:M_PI endAngle:0 clockwise:NO];
maskLayer = [CAShapeLayer layer];
maskLayer.backgroundColor = [UIColor clearColor].CGColor;
maskLayer.path = bezierPath1.CGPath;
maskLayer.strokeColor = [UIColor yellowColor].CGColor;
maskLayer.fillColor = [UIColor whiteColor].CGColor;
maskLayer.lineWidth = 20;
maskLayer.fillRule = kCAFillRuleEvenOdd;
maskLayer.lineCap = kCALineCapRound;
[self.view.layer addSublayer:maskLayer];
maskLayer.strokeStart = 0;
maskLayer.strokeEnd = 1;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.fromValue = @0;
animation.duration = 2.0f;
animation.repeatCount = 0;
[animation setValue:@"BasicAnimationEn" forKey:@"animationName"];
[maskLayer addAnimation:animation forKey:@"BasicAnimationEn"];
}
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
if (!_animation) {
CGPoint center = self.view.center;
CGFloat x = center.x;
CGFloat y = center.y;
UIBezierPath *bezierPath2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(x-15, y-30) radius:5 startAngle:0 endAngle:M_PI * 2 clockwise:YES];
maskLayer = [CAShapeLayer layer];
maskLayer.backgroundColor = [UIColor clearColor].CGColor;
maskLayer.path = bezierPath2.CGPath;
maskLayer.strokeColor = [UIColor blackColor].CGColor;
maskLayer.fillColor = [UIColor whiteColor].CGColor;
maskLayer.lineWidth = 5;
[self.view.layer addSublayer:maskLayer];
maskLayer.strokeStart = 0;
maskLayer.strokeEnd = 1;
_animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
_animation.fromValue = @0;
_animation.delegate = self;
_animation.duration = 1.0f;
[_animation setValue:@"BasicAnimationEnd" forKey:@"animationName"];
[maskLayer addAnimation:_animation forKey:@"BasicAnimationEnd"];
{
CGPoint center = self.view.center;
CGFloat x = center.x;
CGFloat y = center.y;
UIBezierPath *bezierPath_right = [UIBezierPath bezierPathWithArcCenter:CGPointMake(x
+15, y-30) radius:5 startAngle:0 endAngle:M_PI * 2 clockwise:NO];
maskLayer = [CAShapeLayer layer];
maskLayer.backgroundColor = [UIColor clearColor].CGColor;
maskLayer.path = bezierPath_right.CGPath;
maskLayer.strokeColor = [UIColor blackColor].CGColor;
maskLayer.fillColor = [UIColor whiteColor].CGColor;
maskLayer.lineWidth = 5;
[self.view.layer addSublayer:maskLayer];
_animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
_animation.fromValue = @0;
_animation.delegate = self;
_animation.duration = 1.0f;
[_animation setValue:@"BasicAnimationEnd" forKey:@"animationName"];
[maskLayer addAnimation:_animation forKey:@"BasicAnimationEnd"];
}
}
}
Simulator Screen Shot - iPhone 11 Pro Max - 2020-04-13 at 17.36.01.png
网友评论