UI进阶--隐式动画
隐式动画:直接改变属性就会有动画效果,非根层才有隐式动画,根层是没有隐式动画的。
根层与非根层:
控件的layer属性是根层
控件的layer属性的子层就是非根层
隐藏动画的禁止:
1 [CATransaction begin]; 2 [CATransaction setDisableActions:YES]; 3 //设置隐式动画动画时间 4 self.myview.layer.position = CGPointMake(10, 10); 5 [CATransaction commit]; 6
布局:
示例代码:
1 // 2 // ViewController.m 3 // ImplicitAnimation 4 // 5 // Created by xiaomoge on 15-1-5. 6 // Copyright (c) 2015年 xiaomoge. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 @property (weak, nonatomic) IBOutlet UIImageView *imageView; 13 @property (weak, nonatomic) CALayer *layer;//非根层 14 15 @end 16 17 @implementation ViewController 18 19 - (void)viewDidLoad { 20 [super viewDidLoad]; 21 //创建一个 非根层 22 CALayer *layer = [CALayer layer]; 23 //设置内容 24 layer.contents = (id)[UIImage imageNamed:@"papa"].CGImage; 25 //设置大小 26 layer.bounds = CGRectMake(0, 0, 100, 100); 27 28 //设置位置 29 layer.position = CGPointMake(150, 250); 30 31 //设置锚点 32 layer.anchorPoint = CGPointMake(0.5, 0.5); 33 34 [self.view.layer addSublayer:layer]; 35 36 self.layer = layer; 37 } 38 39 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 40 //隐藏动画 41 42 //self.imageView.layer 它是根层 43 //根层改变属性是没有动画,只有非根层改变属性就有默认的动画 44 //设置根层的大小 45 self.imageView.layer.bounds = CGRectMake(0, 0, 150, 150); 46 47 //设置非根层的大小 48 //想默认动画时间设置长一点 49 50 [CATransaction begin]; 51 [CATransaction setDisableActions:YES];//关闭隐藏动画 52 [CATransaction setAnimationDuration:5]; 53 54 //transform旋转的动画效果 55 //self.layer.bounds = CGRectMake(0, 0, 150, 150); 56 self.layer.transform = CATransform3DMakeRotation(M_PI_4, 1, 1, 1); 57 58 [CATransaction commit]; 59 } 60 @end