- (void)addRingLayer {
CAShapeLayer *ringLayer = [CAShapeLayer layer];
ringLayer.frame = CGRectMake(100, 100, 100, 100);
UIBezierPath *path = [UIBezierPath bezierPath];
//不能是CGPointMake(150, 150),因为path在ringLayer的内部
[path addArcWithCenter:CGPointMake(50, 50) radius:50 startAngle:-0.5 * M_PI endAngle:1.5 * M_PI clockwise:YES];
ringLayer.path = path.CGPath;
ringLayer.lineWidth = 2;
ringLayer.strokeColor = UIColor.redColor.CGColor;
// ringLayer.fillColor = UIColor.greenColor.CGColor;
ringLayer.fillColor = nil;
ringLayer.backgroundColor = nil;
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotationAnimation.byValue = @(2.0 * M_PI);
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
CABasicAnimation *strokeStartAnimation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
strokeStartAnimation.fromValue = @(0);
strokeStartAnimation.toValue = @(1);
strokeStartAnimation.duration = 1.2;
strokeStartAnimation.beginTime = 0.5;
strokeStartAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.4 :0.0 :0.2 :1.0];
CABasicAnimation *strokeEndAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
strokeEndAnimation.fromValue = @(0);
strokeEndAnimation.toValue = @(1);
strokeEndAnimation.duration = 0.7;
strokeEndAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.4 :0.0 :0.2 :1.0];
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[rotationAnimation, strokeEndAnimation, strokeStartAnimation];
//不能是strokeStartAnimation.duration + strokeEndAnimation.duration,否则会看到一个完整的环停留一下。
group.duration = strokeStartAnimation.duration + strokeStartAnimation.beginTime;
group.removedOnCompletion = NO;
group.repeatCount = HUGE_VAL;
group.fillMode = kCAFillModeForwards;
[ringLayer addAnimation:group forKey:@"animation"];
[self.view.layer addSublayer:ringLayer];
// UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
// greenView.backgroundColor = UIColor.greenColor;
// [self.view addSubview:greenView];
}
网友评论