首先了解一下CALayer的基本经常使用的属性:ide
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 // Do any additional setup after loading the view, typically from a nib. 4 5 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; 6 7 button.frame = CGRectMake(100, 50, 175, 175); 8 9 [button setBackgroundImage:[UIImage imageNamed:@"06.jpg"] forState:UIControlStateNormal]; 10 11 [self.view addSubview:button]; 12 13 button.layer.cornerRadius = 20 ; 14 15 button.layer.borderColor = [[UIColor yellowColor]CGColor]; 16 17 button.layer.borderWidth = 2 ; 18 19 // button.clipsToBounds = YES ; 20 21 //阴影颜色 22 button.layer.shadowColor = [[UIColor blackColor]CGColor]; 23 24 //阴影的偏移距离 25 button.layer.shadowOffset = CGSizeMake(20, 20); 26 27 //阴影的透明度 28 button.layer.shadowOpacity = 0.8 ; 29 30 //阴影 31 button.layer.shadowRadius = 20 ; 32 33 //是否裁剪边框以外的 34 // button.layer.masksToBounds = YES ; 35 36 37 //CALayer负责视图的渲染 UI中真正负责绘图的部分 没有用户交互,仅仅是展现视图内容,通常用来作动画 38 CALayer *layer = [CALayer layer]; 39 40 layer.frame = CGRectMake(100, 300, 175, 175); 41 42 layer.backgroundColor = [[UIColor yellowColor]CGColor]; 43 44 layer.contents = (id)[[UIImage imageNamed:@"06.jpg"]CGImage]; 45 46 [self.view.layer addSublayer:layer]; 47 48 layer.shadowOffset = CGSizeMake(20, 20); 49 50 layer.shadowColor = [[UIColor redColor]CGColor]; 51 52 layer.shadowOpacity = 0.3 ; 53 54 55 }
以上就是CALayer经常使用的属性,接下来咱们用CALayer写个动画的小Demo动画
1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @property (nonatomic,retain) CALayer *layer ; 6 7 @end 8 9 @implementation ViewController 10 11 -(void)dealloc{ 12 13 [_layer release]; 14 [super dealloc]; 15 } 16 17 - (void)viewDidLoad { 18 [super viewDidLoad]; 19 // Do any additional setup after loading the view, typically from a nib. 20 21 self.layer = [CALayer layer]; 22 23 self.layer.bounds = CGRectMake(0, 0, 100, 100); 24 25 //设置锚点 26 self.layer.position = CGPointMake(187.5, 200); 27 28 //设置锚点在layer上的比例 29 self.layer.anchorPoint = CGPointMake(0.2, 0.2) ; 30 31 self.layer.cornerRadius = 20 ; 32 33 self.layer.borderWidth = 2 ; 34 35 self.layer.shadowOffset = CGSizeMake(20, 20); 36 37 self.layer.shadowOpacity = 0.2 ; 38 39 self.layer.doubleSided = YES ; 40 41 self.layer.borderColor = [[UIColor redColor]CGColor]; 42 43 self.layer.backgroundColor = [[UIColor brownColor]CGColor]; 44 45 [self.view.layer addSublayer:self.layer]; 46 47 [_layer release]; 48 49 } 50 51 - (void)didReceiveMemoryWarning { 52 [super didReceiveMemoryWarning]; 53 // Dispose of any resources that can be recreated. 54 } 55 56 57 #pragma mark -- 平移CALayer 58 - (IBAction)buttonAction1:(UIButton *)sender { 59 60 //建立一个基本动画对象 61 CABasicAnimation *animation = [CABasicAnimation animation]; 62 63 //动画的类型 64 //keyPath layer的某个属性 65 animation.keyPath = @"position"; 66 67 //动画的持续时间 68 animation.duration = 2 ; 69 70 //toValue 就是一个临时值,经过CALayer作的动画不会改变layer属性自己的值 71 animation.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 400)]; 72 73 //若是要保留动画的最终目的,下面两个属性分都须要设置 74 75 //保持动画的最新状态 76 animation.fillMode = kCAFillModeBackwards ; 77 78 //设置动画完成后以后不删除 79 animation.removedOnCompletion = NO ; 80 81 animation.delegate = self ; 82 83 [self.layer addAnimation:animation forKey:nil]; 84 85 86 87 } 88 89 -(void)animationDidStart:(CAAnimation *)anim 90 { 91 92 NSLog(@"%@",NSStringFromCGRect(self.layer.bounds)); 93 94 } 95 96 97 #pragma mark --实现缩放 98 - (IBAction)buttonAction2:(UIButton *)sender { 99 100 //1.建立一个基本动画 101 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"]; 102 103 //2.设置动画持续时间 104 animation.duration = 2 ; 105 106 //3.设置动画的最终效果 107 animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)]; 108 109 //4.使动画保留最终状态 110 animation.fillMode = kCAFillModeForwards ; 111 112 animation.removedOnCompletion = NO ; 113 114 //5.将动画对象付给layer 115 [self.layer addAnimation:animation forKey:nil]; 116 117 } 118 119 - (IBAction)buttonAction3:(UIButton *)sender { 120 121 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"]; 122 123 animation.duration = 2 ; 124 125 animation.fillMode = kCAFillModeForwards ; 126 127 animation.removedOnCompletion = NO ; 128 129 animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI / 3, 0, 0, 1)]; 130 131 [self.layer addAnimation:animation forKey:nil]; 132 133 134 }