UI基础 - CAAnination:关键帧动画 | 基础动画 | 过渡动画

CAAnination

1 - CAAnination 类中封装了 iOS 中所有的动画效果,其动画都是添加在 CALayer 上的,是动画的触发核心,它是抽象父类,通常使用其子类实现动画效果

2 - 它的三个子类分别是:CAPropertyAnimation、CAAnimationGroup、CATransition。其中 CAPropertyAnimation 同样是抽象类,有两个子类 CABasicAnimation、CAKeyFrameAnimation,用来给 CALayer 部分属性添加动画

3 - 常用 API

①  给 layer 添加、移除动画

- (void)addAnimation:(CAAnimation *)anim forKey:(NSString *)key;
- (void)removeAnimationForKey:(NSString *)key;
- (void)removeAllAnimations;

② CABasicAnimation 通过设定初始值和结束值执行动画

+ (id)animationWithKeyPath:(NSString *)path; //系统提供的构造方法
@property(copy) NSString *keyPath; // 只能填写 CALayer 动画中能够用动画的属性名
@property(retain) id fromValue;// 起始值
@property(retain) id toValue;  // 结束值
@property(retain) id byValue;  // 相对值

③ CAKeyFrameAnimation:关键帧动画可以让你的 view 的 layer 按照预定轨迹做动画

+ (id)animationWithKeyPath:(NSString *)path;// 构造方法
@property CGPathRef path;// 动画路径
@property(copy) NSArray *values;  // 提供了一组关键帧的值,当使用 path 的时候,values 会被忽略
@property(copy) NSArray *keyTimes;// 每一帧的时间,其成员必须是 NNSNumber
@property(copy) NSString *rotationMode;// 设定关键帧中的值如何计算

④ CAAnimationGroup 只有一个数组属性:添加多个 CAAnimation 一起执行

⑤ CATransition:用作 layer 动画过渡,它有两个主要属性:type(动画过渡效果)和 subType(动画过渡方向)

4 - 代码示例

  1 #import "ViewController.h"
  2 @interface ViewController ()
  3 // 动画视图
  4 @property(nonatomic,strong)UIView *animationView;
  5 @end
  6 
  7 @implementation ViewController
  8 
  9 - (void)viewDidLoad {
 10     [super viewDidLoad];
 11 
 12     self.view.backgroundColor = [UIColor redColor];
 13     
 14     // animationView
 15     self.animationView = [[UIView alloc] initWithFrame:CGRectMake(30, 50, 90, 80)];
 16     self.animationView.backgroundColor = [UIColor cyanColor];
 17     [self.view addSubview:self.animationView];
 18     // 初始化状态
 19     self.animationView.layer.cornerRadius = 20;
 20     self.animationView.layer.borderWidth = 5;
 21     self.animationView.layer.borderColor = [UIColor yellowColor].CGColor;
 22     self.animationView.layer.shadowOpacity = 1.0;
 23     self.animationView.layer.shadowOffset = CGSizeMake(8, 8);           // 阴影偏移量
 24     self.animationView.layer.shadowColor = [UIColor greenColor].CGColor;// 阴影颜色
 25 
 26     // 基本动画
 27     // [self testBasicAnimation];
 28 
 29     // 关键帧动画
 30     // [self testKeyFrameAnimation];
 31 
 32     // 过渡动画
 33     // [self testTransitionAnimation];
 34 
 35 }
 36 
 37 // 基本动画
 38 -(void)testBasicAnimation{
 39 
 40 //---------------------------------CABasicAnimation
 41 //    // 效果一:控制 y 的值
 42 //    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
 43 //    basicAnimation.duration = 1.0;
 44 //    basicAnimation.repeatCount = 1;
 45 //    basicAnimation.autoreverses = YES;
 46 //    // 动画的起始值
 47 //    basicAnimation.fromValue = [NSNumber numberWithFloat:90];
 48 //    // 动画的结束值
 49 //    basicAnimation.toValue = [NSNumber numberWithFloat:200];
 50 //    // 添加动画
 51 //    [self.animationView.layer addAnimation:basicAnimation forKey:nil];
 52 
 53 
 54 //    // 效果二:控制 position
 55 //    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
 56 //    basicAnimation.duration = 1.0;
 57 //    basicAnimation.repeatCount = 1;
 58 //    basicAnimation.autoreverses = YES;
 59 //    basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(160, 90)];
 60 //    basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)];
 61 //    [self.animationView.layer addAnimation:basicAnimation forKey:nil];
 62 
 63 
 64 //    // 效果三:颜色渐变
 65 //    CABasicAnimation *animationColor = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
 66 //    animationColor.duration = 1.0;
 67 //    animationColor.repeatCount = 1;
 68 //    animationColor.autoreverses = YES;
 69 //    animationColor.fromValue = (id)[UIColor orangeColor].CGColor;
 70 //    animationColor.toValue = (id)[UIColor cyanColor].CGColor;
 71 //    [self.animationView.layer addAnimation:animationColor forKey:nil];
 72 
 73 
 74 //------------------------------------CAAnimationGroup
 75     // 效果二:控制 position
 76     CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
 77     basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(160, 90)];
 78     basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)];
 79 
 80     // 效果三:颜色渐变
 81     CABasicAnimation *animationColor = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
 82     animationColor.fromValue = (id)[UIColor orangeColor].CGColor;
 83     animationColor.toValue = (id)[UIColor cyanColor].CGColor;
 84 
 85 
 86     // 注:效果二、三,我们可以使用 CAAnimationGroup 进行优化
 87     // 重复的属性有 duration、autoreverses、repeatCount 以及两个 addAnimation
 88     CAAnimationGroup *group = [CAAnimationGroup animation];
 89     group.animations  = @[basicAnimation,animationColor];
 90     // 重复的属性
 91     group.duration = 2;
 92     group.repeatCount = 2;
 93     group.autoreverses = YES;
 94     [self.animationView.layer addAnimation:group forKey:nil];
 95 
 96  }
 97 
 98 
 99 // 关键帧动画
100 - (void)testKeyFrameAnimation{
101 
102     CAKeyframeAnimation * keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
103     // 指定 6 个帧数
104     keyFrameAnimation.values = @[[NSValue valueWithCGPoint:CGPointMake(50, 50)],
105                                  [NSValue valueWithCGPoint:CGPointMake(200, 400)],
106                                  [NSValue valueWithCGPoint:CGPointMake(180, 210)],
107                                  [NSValue valueWithCGPoint:CGPointMake(100, 300)],
108                                  [NSValue valueWithCGPoint:CGPointMake(50, 50)],
109                                  [NSValue valueWithCGPoint:CGPointMake(200, 400)]];
110 
111     keyFrameAnimation.duration = 15;// 默认每个动画时间 15.0 /(6.0-1.0)
112     // 每帧结束占总时长百分比:
113     keyFrameAnimation.keyTimes = @[@0,@0.1,@0.2,@0.65,@0.8,@1.0];// 每个间段所占比 = 后一个的比 - 前一个的比
114     keyFrameAnimation.repeatCount = 1;
115     [self.animationView.layer addAnimation:keyFrameAnimation forKey:nil];
116 }
117 
118 
119 // 过渡动画
120  - (void)testTransitionAnimation{
121 
122     CATransition * transition = [CATransition animation];
123     transition.type = @"moveIn";
124     transition.duration = 2.0;
125 
126     // 代理:监听开始和结束
127     // transition.delegate = self;// 此处不在演示,使用时引入头文件、实现代理方法即可
128     transition.subtype = kCATransitionFromRight;
129     // 注意,使用的是 self.view
130     [self.view.layer addAnimation:transition forKey:nil];
131 
132     /*----------- 动画效果
133 
134         @"cube"                     立方体翻滚效果
135         @"moveIn"                   新视图移到旧视图上面
136         @"reveal"                   显露效果(将旧视图移开,显示下面的新视图)
137         @"fade"                     交叉淡化过渡(不支持过渡方向)             (默认为此效果
138         @"pageCurl"                 向上翻一页
139         @"pageUnCurl"               向下翻一页
140         @"suckEffect"               收缩效果,类似系统最小化窗口时的神奇效果(不支持过渡方向)
141         @"rippleEffect"             滴水效果,(不支持过渡方向)
142         @"oglFlip"                  上下左右翻转效果
143         @"rotate"                   旋转效果
144         @"push"
145         @"cameraIrisHollowOpen"     相机镜头打开效果(不支持过渡方向)
146         @"cameraIrisHollowClose"    相机镜头关上效果(不支持过渡方向)
147      */
148  }
149  @end

运行效果:初始视图

 

posted on 2018-04-16 17:52  低头捡石頭  阅读(20)  评论(0编辑  收藏  举报

导航