ios核心动画之基本动画

  •  Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理api,使用它能做出非常绚丽的动画效果,而且往往事半功倍。也就是说食用少量的代码就能实现非常强大的功能。
  •  Core Animation可以用在Mac OS X和IOS平台
  •  Core Animation的动画执行过程都是在后台操作的,不会阻塞主线程。
  •  要注意的是,Core Animation是直接作用在CALayer上的,并非UIView

 Core Animation的使用步骤

  1. 要先添加QuartzCore.framework框架和引入主头文件<QuartzCore/QuartzCore.h>(ios7不需要)
  2. 初始化一个CAAnimation对象,并设置一些动画相关属性
  3. 通过调用CALayer的addAimation:forKey:方法增加CAAnimation对象到CALayer中,这样就能开始执行动画了
  4. 通过调用CALayer的removeAnimationForKey:方法可以停止CALAYER的动画
  1 @interface NJViewController ()
  2 
  3 @property (nonatomic, strong) CALayer *myLayer;
  4 
  5 @end
  6 
  7 @implementation NJViewController
  8 
  9 - (void)viewDidLoad
 10 {
 11     [super viewDidLoad];
 12     // 1.创建layer
 13     CALayer *myLayer = [CALayer layer];
 14     myLayer.bounds = CGRectMake(0, 0, 100, 100);
 15     
 16     myLayer.anchorPoint = CGPointZero;
 17     myLayer.position = CGPointMake(100, 100);
 18     myLayer.backgroundColor = [UIColor greenColor].CGColor;
 19     // 2.将自定义Layer添加到控制器的view的layer上
 20     [self.view.layer addSublayer:myLayer];
 21     
 22     self.myLayer = myLayer;
 23 
 24 }
 25 
 26 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 27 {
 28     // 1. 创建核心动画
 29     CABasicAnimation *anima = [CABasicAnimation animation] ;
 30     // 1.1设置动画类型
 31 //    anima.keyPath = @"transform.translation.x";
 32     anima.keyPath = @"transform.scale.y";
 33     
 34     // 1.2 设置动画执行完毕之后不删除动画
 35     anima.removedOnCompletion = NO;
 36     // 1.3 设置保存动画的最新状态
 37     anima.fillMode = kCAFillModeForwards;
 38     // 1.4设置动画时间
 39     anima.duration = 1;
 40     
 41     // 1.5如何动画
 42 //    anima.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, 100, 1)];
 43 //    anima.toValue = @(100);
 44     anima.toValue = @(1.5);
 45  
 46     
 47     // 2.添加核心动画到Layer
 48     [self.myLayer addAnimation:anima forKey:nil];
 49 
 50 }
 51 
 52 - (void)test2
 53 {
 54     // 1. 创建核心动画
 55     CABasicAnimation *anima = [CABasicAnimation animation] ;
 56     // 1.1设置动画类型
 57     anima.keyPath = @"transform";
 58     
 59     
 60     // 1.2 设置动画执行完毕之后不删除动画
 61     anima.removedOnCompletion = NO;
 62     // 1.3 设置保存动画的最新状态
 63     anima.fillMode = kCAFillModeForwards;
 64     // 1.4设置动画时间
 65     anima.duration = 1;
 66     
 67     // 1.5修改动画
 68     anima.toValue =  [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)];
 69     
 70     // 2.添加核心动画到Layer
 71     [self.myLayer addAnimation:anima forKey:nil];
 72 }
 73 
 74 
 75 - (void)test1
 76 {
 77     // 1. 创建核心动画
 78     CABasicAnimation *anima = [CABasicAnimation animation] ;
 79     // 1.1设置动画类型
 80     anima.keyPath = @"bounds";
 81     
 82     // 1.2 设置动画执行完毕之后不删除动画
 83     anima.removedOnCompletion = NO;
 84     // 1.3 设置保存动画的最新状态
 85     anima.fillMode = kCAFillModeForwards;
 86     // 1.4设置动画时间
 87     anima.duration = 1;
 88     
 89     // 1.5修改动画
 90     anima.toValue =[NSValue valueWithCGRect: CGRectMake(0, 0, 200, 200)];
 91     
 92     // 2.添加核心动画到Layer
 93     [self.myLayer addAnimation:anima forKey:nil];
 94 }
 95 
 96 - (void)test
 97 {
 98     // 1. 创建核心动画
 99     CABasicAnimation *anima = [CABasicAnimation animation] ;
100     // 1.1告诉系统要执行什么样的动画
101     anima.keyPath = @"position";
102     // 设置通过动画将layer从哪
103     //    anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
104     // 到哪(到指定的位置)
105     anima.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)];
106     //    在当前位置的基础上增加多少
107     //    anima.byValue = [NSValue valueWithCGPoint:CGPointMake(0, 300)];
108     
109     // 设置动画时间
110     anima.duration = 5;
111     
112     // 1.2 设置动画执行完毕之后不删除动画
113     anima.removedOnCompletion = NO;
114     // 1.3 设置保存动画的最新状态
115     anima.fillMode = kCAFillModeForwards;
116     
117     // 2.添加核心动画到Layer
118     [self.myLayer addAnimation:anima forKey:nil];
119 }
120 
121 
122 @end

 

posted @ 2016-01-24 11:26  xiaocaoera  阅读(189)  评论(0编辑  收藏  举报