[关闭]
@zwenqiang 2015-12-17T16:55:51.000000Z 字数 3684 阅读 7192

IOS动画之Core Animation基础

IOS

首先附上一张从网上找到的Core Animation的结构图。
Core Animaion

准备: 使用Core Animation制作动画需要引入QuartzCore.framework库,并在制作动画的地方引入#import <QuartzCore/QuartzCore.h>

CABasicAnimation(关键帧动画)

所谓关键帧动画,就是将Layer的属性作为KeyPath来注册,指定动画的起始帧和结束帧,然后自动计算和实现中间的过渡动画的一种动画方式。

  1. CABasicAnimation *animation = [CABasicAnimation animation];
  2. animation.keyPath = @"position.x";
  3. animation.duration = 1;
  4. animation.fromValue = @77;
  5. animation.toValue = @455;
  6. animation.repeatCount = 1;
  7. animation.beginTime = CACurrentMediaTime() + 0.5;
  8. animation.autoreverses = YES;
  9. [rocket.layer addAnimation:animation forKey:@"basic"];

动画属性参数说明:

常见的支持有: 见官方文档

平移:
- position(点到点坐标)
- position.x或者transform.translation.x(x坐标方向的移动)
- position.y或者transform.translation.y(y坐标方向的移动)
缩放:scale
旋转:rotation
- transform.rotation.x(绕x坐标轴旋转)
- transform.rotation.y(绕y坐标轴旋转)

  1. animation.keyPath = @"position";
  2. animation.keyPath = @"transform.scale"
  3. animation.keyPath = @"transform.rotation.y"

动画中fromValue -> toValue表示从开始位置到结束位置(相对位置),也可以设定从
当前位置到加上byValue. fromValuetoValuebyValueid类型,如果是基本类型的话,需要转化为OC对应的类类型@。

  1. animation.fromValue = [NSNumber numberWithFloat:50.0];
  2. animation.toValue = @500.0;
  3. animation.byValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];
  1. animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]; //先加速后减速
动画结束之后,动画界面又恢复动画前的状态。要想保持动画后的状态,可以有以下两种方式

方法一(推荐)
这种方式有局限性,适用于消失效果的动画。

  1. layer.position = CGPointMake(455, 61);

方法二(通用)
可以添加以下两行代码

  1. animation.fillMode = kCAFillModeForward;
  2. animation.removedOnCompletion = NO;

默认动画执行结束之后,就从渲染树render tree中移除,设置removedOnCompletionNO,保留渲染后的动画。

CAKeyframeAnimation组合动画

CABasicAnimation动画是一次执行单一效果的动画,CAKeyframeAnimation可以在一次动作中执行多种效果。

  1. 为动画图形运动划定运动路线
  2. //定义动画贝塞尔曲线路径
  3. UIBezierPath *path = [UIBezierPath bezierPath];
  4. //定义起点
  5. [path moveToPoint:CGPointMake(50, 150)];
  6. //结束点、中间点
  7. [path addQuadCurveToPoint:CGPointMake(270, 300) controlPoint:CGPointMake(150, 20)];
  8. //定义2个路径点
  9. //[path addCurveToPoint:CGPointMake(50, 500) controlPoint1: CGPointMake(350, 400) controlPoint2:CGPointMake(100, 450)];
  10. CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
  11. animation.path = path.CGPath; //设置路径
  12. animation.rotationMode = kCAAnimationRotateAuto;
  13. //放大
  14. CABasicAnimation *expandAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  15. expandAnimation.duration = 0.5f;
  16. expandAnimation.fromValue = [NSNumber numberWithFloat:1];
  17. expandAnimation.toValue = [NSNumber numberWithFloat:2.0f];
  18. expandAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
  19. //缩小
  20. CABasicAnimation *narrowAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  21. narrowAnimation.beginTime = 0.5;
  22. narrowAnimation.fromValue = [NSNumber numberWithFloat:2.0f];
  23. narrowAnimation.duration = 1.5f;
  24. narrowAnimation.toValue = [NSNumber numberWithFloat:0.5f];
  25. narrowAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  26. //添加动画集
  27. CAAnimationGroup *groups = [CAAnimationGroup animation];
  28. groups.animations = @[animation,expandAnimation,narrowAnimation];
  29. groups.duration = 2.0f;
  30. groups.removedOnCompletion = NO;
  31. groups.fillMode = kCAFillModeForwards;
  32. groups.delegate = self;
  33. [layer addAnimation:groups forKey:@"group"];

监听动画执行开始和结束的状态CAAnimationDelegate

指定委托对象,实现委托方法
animation.delegate = self; // 指定委托对象

  1. #pragma mark CAAnimation Delegate
  2. - (void)animationDidStart:(CAAnimation *)anim{
  3. NSLog(@"动画启动");
  4. }
  5. - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
  6. NSLog(@"动画结束");
  7. }

IOS 动画效果的开源库

参考:
Animations Explained / 中文翻译
CSDN博客 - CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
深入了解
- Further Reading
- UIKit Dynamics

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注