实现如果的动画方式大概有两种:
2.自己用代码绘制然后加上动画
代码主要用到:
CALayer+UIBezierPath+CAShapeLayer+CAGradientLayer+CABasicAnimation
1.创建一个图层
_layer = [CALayer layer];
_layer.frame = CGRectMake(100, 100, 110, 110);
_layer.backgroundColor = [UIColor blueColor].CGColor;
image.png
2.绘制一个路径,再生成一个背景圆环加到view上
//创建圆环
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(55, 55) radius:50 startAngle:0 endAngle:M_PI * 2 clockwise:YES];
//圆环遮罩
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.strokeColor = [UIColor whiteColor].CGColor;
shapeLayer.lineWidth = 5;
shapeLayer.strokeStart = 0;
shapeLayer.strokeEnd = 1;
shapeLayer.lineCap = kCALineCapRound;
shapeLayer.lineDashPhase = 0.8;
shapeLayer.path = bezierPath.CGPath;
//这里一定要用mask 否者会有问题
[_layer setMask:shapeLayer];
image.png
3.利用CAGradientLayer绘制渐变的颜色
由于CAGradientLayer是坐标到坐标之间的渐变,需要什么效果需要自己定制
渐变成坐标如下:
image.png
//颜色渐变
NSMutableArray *colors = [NSMutableArray arrayWithObjects:(id)[UIColor blueColor].CGColor,(id)[RYUIColorWithRGB(255, 255, 255, 0.5) CGColor], nil];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.shadowPath = bezierPath.CGPath;
gradientLayer.frame = CGRectMake(0, 0, 110, 55);
gradientLayer.startPoint = CGPointMake(1, 0);
gradientLayer.endPoint = CGPointMake(0, 0);
[gradientLayer setColors:[NSArray arrayWithArray:colors]];
NSMutableArray *colors1 = [NSMutableArray arrayWithObjects:(id)[RYUIColorWithRGB(255, 255, 255, 0.5) CGColor],(id)[[UIColor whiteColor] CGColor], nil];
CAGradientLayer *gradientLayer1 = [CAGradientLayer layer];
gradientLayer1.shadowPath = bezierPath.CGPath;
gradientLayer1.frame = CGRectMake(0, 55, 110, 55);
gradientLayer1.startPoint = CGPointMake(0, 1);
gradientLayer1.endPoint = CGPointMake(1, 1);
[gradientLayer1 setColors:[NSArray arrayWithArray:colors1]];
[_layer addSublayer:gradientLayer]; //设置颜色渐变
[_layer addSublayer:gradientLayer1];
作者:洁简
链接:https://www.jianshu.com/p/f3754aa9dbcd
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
代码效果如下
image.png
最后叠加效果
image.png
4.利用CABasicAnimation实现简单的旋转
- (void)animation {
//动画
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.fromValue = [NSNumber numberWithFloat:0];
rotationAnimation.toValue = [NSNumber numberWithFloat:2.0*M_PI];
rotationAnimation.repeatCount = MAXFLOAT;
rotationAnimation.duration = 1;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[_layer addAnimation:rotationAnimation forKey:@"rotationAnnimation"];
}
网友评论