标签:
2
1 // 2 // ViewController.m 3 // coreAnimation 4 // 5 // Created by ys on 15/11/21. 6 // Copyright (c) 2015年 ys. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 @property (nonatomic,strong)CALayer *layer; 13 @end 14 15 @implementation ViewController 16 17 - (void)viewDidLoad 18 { 19 [super viewDidLoad]; 20 21 CALayer *layer = [CALayer layer]; 22 layer.position = CGPointMake(100, 100); 23 layer.bounds = CGRectMake(0, 0, 100, 100); 24 layer.backgroundColor = [UIColor redColor].CGColor; 25 [self.view.layer addSublayer:layer]; 26 self.layer = layer; 27 } 28 29 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 30 { 31 // [self testTranslate]; 32 // [self testRotate]; 33 // [self testScale]; 34 [self testTransform]; 35 } 36 37 - (void)testTransform 38 { 39 // 1.创建动画对象 40 CABasicAnimation *anim = [CABasicAnimation animation]; 41 42 // 2.设置动画对象 43 // keyPath决定了执行怎样的动画, 调整哪个属性来执行动画 44 // anim.keyPath = @"transform.rotation"; 45 // anim.keyPath = @"transform.scale.x"; 46 anim.keyPath = @"transform.translation.x"; 47 anim.toValue = @(100); 48 // anim.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)]; 49 anim.duration = 2.0; 50 51 anim.removedOnCompletion = NO; 52 anim.fillMode = kCAFillModeForwards; 53 54 // 3.添加动画 55 [self.layer addAnimation:anim forKey:nil]; 56 } 57 58 - (void)testRotate 59 { 60 // 1.创建动画对象 61 CABasicAnimation *anim = [CABasicAnimation animation]; 62 63 // 2.设置动画对象 64 // keyPath决定了执行怎样的动画, 调整哪个属性来执行动画 65 anim.keyPath = @"transform"; 66 // anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)]; 67 anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1, -1, 0)]; 68 anim.duration = 2.0; 69 70 anim.removedOnCompletion = NO; 71 anim.fillMode = kCAFillModeForwards; 72 73 // 3.添加动画 74 [self.layer addAnimation:anim forKey:nil]; 75 } 76 77 - (void)testScale 78 { 79 // 1.创建动画对象 80 CABasicAnimation *anim = [CABasicAnimation animation]; 81 82 // 2.设置动画对象 83 // keyPath决定了执行怎样的动画, 调整哪个属性来执行动画 84 anim.keyPath = @"bounds"; 85 // anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)]; 86 anim.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)]; 87 anim.duration = 2.0; 88 89 /**让图层保持动画执行完毕后的状态**/ 90 // 动画执行完毕后不要删除动画 91 anim.removedOnCompletion = NO; 92 // 保持最新的状态 93 anim.fillMode = kCAFillModeForwards; 94 95 // 3.添加动画 96 [self.layer addAnimation:anim forKey:nil]; 97 } 98 99 100 - (void)testTranslate 101 { 102 // 1.创建动画对象 103 CABasicAnimation *anim = [CABasicAnimation animation]; 104 105 // 2.设置动画对象 106 // keyPath决定了执行怎样的动画, 调整哪个属性来执行动画 107 anim.keyPath = @"position"; 108 // anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)]; 109 // toValue : 最终变成什么值 110 // byValue : 增加多少值 111 anim.byValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)]; 112 anim.duration = 2.0; 113 114 /**让图层保持动画执行完毕后的状态**/ 115 // 动画执行完毕后不要删除动画 116 anim.removedOnCompletion = NO; 117 // 保持最新的状态 118 anim.fillMode = kCAFillModeForwards; 119 120 // 3.添加动画 121 [self.layer addAnimation:anim forKey:nil]; 122 } 123 124 125 126 @end
coreAnimation核心动画(一)CABasicAnimation
标签:
原文地址:http://www.cnblogs.com/yangshun-work/p/4985026.html