ios核心動畫之基本動畫

  •  Core Animation,中文翻譯爲核心動畫,它是一組很是強大的動畫處理api,使用它能作出很是絢麗的動畫效果,並且每每事半功倍。也就是說食用少許的代碼就能實現很是強大的功能。
  •  Core Animation能夠用在Mac OS X和IOS平臺
  •  Core Animation的動畫執行過程都是在後臺操做的,不會阻塞主線程。
  •  要注意的是,Core Animation是直接做用在CALayer上的,並不是UIView

 Core Animation的使用步驟ios

  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
相關文章
相關標籤/搜索