小动画
1.画一个❤️图案
CAShapeLayer *shapelayer = [CAShapeLayer layer];
[layer addSublayer:shapelayer];
shapelayer.fillColor = [UIColor redColor].CGColor;
shapelayer.lineWidth = 10;
UIBezierPath *path = [ViewController bezierHeartShapePathWithWidth:80 atPoint:CGPointMake(50, 50)];
shapelayer.path = path.CGPath;
shapelayer.strokeColor = [UIColor redColor].CGColor;
+ (UIBezierPath *)bezierHeartShapePathWithWidth:(CGFloat)width atPoint:(CGPoint)center
{
CGFloat w = width / 2.f;// / 1.3f;
UIBezierPath *path = [[UIBezierPath alloc] init];
[path addArcWithCenter:CGPointMake(center.x - w/2.f, center.y - sqrt(3.f) * w / 6.f) radius:w / sqrt(3.f) startAngle:[self toRadians:150.f] endAngle:[self toRadians:-30.f] clockwise:YES];
[path addArcWithCenter:CGPointMake(center.x + w/2.f, center.y - sqrt(3.f) * w / 6.f) radius:w / sqrt(3.f) startAngle:[self toRadians:-150.f] endAngle:[self toRadians:30.f] clockwise:YES];
[path moveToPoint:CGPointMake(center.x - w, center.y)];
[path addLineToPoint:CGPointMake(center.x, center.y + w*0.6 * sqrt(3.f))];
[path addLineToPoint:CGPointMake(center.x + w, center.y)];
return path;
}
2.缩放
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.duration = 0.8;
animation.fromValue = @(1.0);
animation.toValue = @(1.5);
animation.autoreverses = YES;
animation.repeatCount = CGFLOAT_MAX;
3.旋转
CABasicAnimation *animation3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
animation3.fromValue = @(0);
animation3.toValue = @(6*M_PI);
animation3.duration = 0.2;
animation3.repeatCount = CGFLOAT_MAX;
4.位移
CAKeyframeAnimation *animation2 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation2.values = @[
[NSValue valueWithCGPoint:layer.position],
[NSValue valueWithCGPoint:CGPointMake(layer.position.x+200, layer.position.y)],
[NSValue valueWithCGPoint:CGPointMake(layer.position.x+200, layer.position.y+300)],
[NSValue valueWithCGPoint:CGPointMake(layer.position.x, layer.position.y+300)],
[NSValue valueWithCGPoint:layer.position]
];
animation2.keyTimes = @[
@(0),@(0.2),@(0.5),@(0.7),@(1)
];
animation2.autoreverses = NO;
5.添加到一个CAAnimationGroup
CAAnimationGroup *aniGroup = [CAAnimationGroup animation];
aniGroup.duration = 8;
aniGroup.animations = @[animation,animation2,animation3];
aniGroup.repeatCount = CGFLOAT_MAX;
[layer addAnimation:aniGroup forKey:@"grouani"];
网友评论