Core Animation的使用步驟ios
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