首页 > 代码库 > UI进阶--隐式动画

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 @end16 17 @implementation ViewController18 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

 

UI进阶--隐式动画