核心动画的几个类:
KeyPath的类型
值 | 说明 | 使用形式 |
---|---|---|
transform.scale | 比例 | @(0.5) |
transform.scale.x/y | 宽/高的比例 | @(0.5) |
transform.rotation.x/y/z | 围绕x/y/z轴旋转 | @(M_PI) |
cornerRadius | 圆角的设置 | @(50) |
backgroundColor | 背景颜色的变化 | (id)[UIColor purpleColor].CGColor |
bounds | 大小,中心不变 | [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)] |
position | 位置(中心点的改变) | [NSValue valueWithCGPoint:CGPointMake(300, 300)] |
contents | 内容 | imageAnima.toValue = (id)[UIImage imageNamed:@"to"].CGImage |
opacity | 透明度 | @(0.7) |
contentsRect.size.width | 横向拉伸缩放 | @(0.5) |
path | UIBezierPath的变化 | UIBezierPath .CGPath |
strokeStart | 擦线 | @(0.5) strokeStart默认值为0 |
strokeEnd | 画线 | @(0.5) strokeEnd默认值为1 |
代码
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.delegate = self;
animation.fromValue = (id)[[self halfPath] CGPath];
animation.toValue = (id)[[self nonePath] CGPath];
animation.duration = kAnimationDurationTime;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.delegate = self;
[animation setValue:@"animation1"forKey:@"AnimationKey"];
[layer addAnimation:animation forKey:nil];
setValue:forKey: 用来在代理中区分不同的动画,做不同的处理
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
if ([[anim valueForKey:@"AnimationKey"] isEqualToString:@"animation1"]) {
// 操作 对path进行的动画只是动画过程并没有真是的修改path,动画结束后要重新设置path
CAShapeLayer *shape = layer;
shape.path = [self fullPath].CGPath;
}
}
组合动画
CABasicAnimation *positionAnima = [CABasicAnimation animationWithKeyPath:@"position.y"];
positionAnima.fromValue = @(self.imageView.center.y);
positionAnima.toValue = @(self.imageView.center.y-30);
positionAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
CABasicAnimation *transformAnima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
transformAnima.fromValue = @(0);
transformAnima.toValue = @(M_PI);
transformAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
CAAnimationGroup *animaGroup = [CAAnimationGroup animation];
animaGroup.duration = 2.0f;
animaGroup.fillMode = kCAFillModeForwards;
animaGroup.removedOnCompletion = NO;
animaGroup.animations = @[positionAnima,transformAnima];
[self.imageView.layer addAnimation:animaGroup forKey:@"Animation"];
注意:CAAnimationDelegate用strong修饰,会引起循环引用,动画过程中退出页面时要removeAllAnimations
@property(nullable, strong) id <CAAnimationDelegate> delegate;