1.基本动画
//1.创建动画对象 CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"position.y"]; // 2.设置一些属性 basic.fromValue = @(50); basic.toValue = @(400); // 3.调整时间 默认是0.25s basic.duration = 2; // 4 核心动画结束后不要移除 basic.removedOnCompletion = NO; basic.fillMode = kCAFillModeForwards; // 5.添加 // key 标记, 作用: 用来区别不同的核心动画 可以写任意的字符串,也可以写nil. [self.redBtn.layer addAnimation:basic forKey:nil];
2.关键帧动画
// 1.创建关键帧动画 CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"]; // 2.设置属性 anim.values = @[@(-M_PI_4 * 0.3), @(M_PI_4 * 0.3), @(-M_PI_4 * 0.3)]; anim.repeatCount = CGFLOAT_MAX; anim.duration = 0.15; // 3.添加 [self.redBtn.layer addAnimation:anim forKey:nil];
3.转场动画
@interface HMViewController () //添加控件 @property (weak, nonatomic) IBOutlet UIImageView *imageView; //图片参数 @property (nonatomic, assign) int index; @end @implementation HMViewController - (void)viewDidLoad { [super viewDidLoad]; _index = 1; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { _index++; if (_index == 4) { _index = 1; } NSString *fileName = [NSString stringWithFormat:@"%d",_index]; _imageView.image = [UIImage imageNamed:fileName]; //创建动画对象 CATransition *anim = [CATransition animation]; //设置动画属性 anim.type = @"fromleft"; anim.subtype = kCATransitionFromLeft; anim.startProgress = 0.5; anim.duration = 2; // 添加动画到图层 [_imageView.layer addAnimation:anim forKey:nil]; } @end
4.动画组
//添加基本动画 CABasicAnimation *rotation = [CABasicAnimation animation]; //设置动画属性 rotation.keyPath = @"transform.rotation"; rotation.toValue = @M_PI_2; //添加基本动画 CABasicAnimation *position = [CABasicAnimation animation]; //设置动画属性 position.keyPath = @"position"; position.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 250)]; //添加基本动画 CABasicAnimation *scale = [CABasicAnimation animation]; //设置动画属性 scale.keyPath = @"transform.scale"; scale.toValue = @0.5; //添加组动画对象 CAAnimationGroup *group = [CAAnimationGroup animation]; //设置组动画 group.animations = @[rotation,position,scale]; group.duration = 2; // 取消反弹 group.removedOnCompletion = NO; group.fillMode = kCAFillModeForwards; //添加到图层 [_redView.layer addAnimation:group forKey:nil];
原文:http://www.cnblogs.com/xiejw/p/5204083.html