CALayer隐式动画

每一个UIView内部都默认关联着一个CALayer,我们可用称这个Layer为Root Layer(根层)
所有的非Root Layer,也就是手动创建的CALayer对象,都存在着隐式动画。

什么是隐式动画?

当对非Root Layer的部分属性进行修改时,默认会自动产生一些动画效果,而这些属性称为Animatable Properties(可动画属性)

列举几个常见的Animatable Properties
bounds:用于设置CALayer的宽度和高度。修改这个属性会产生缩放动画。
backgroundColor:用于设置CALayer的背景色。修改这个属性会产生背景色的渐变动画。
position:用于设置CALayer的位置。修改这个属性会产生平移动画。

那么需要怎么才能关闭隐式动画?
通过事务来设置隐式动画开关

#import "ViewController.h"

@interface ViewController ()
/** 自定义的layer层*/
@property (weak, nonatomic) CALayer *layer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CALayer *layer = [[CALayer alloc] init];
    layer.frame = CGRectMake(0, 0, 100, 100);
    layer.backgroundColor = [UIColor redColor].CGColor;
    layer.position = CGPointZero;
    [self.view.layer addSublayer:layer];
    self.layer = layer;
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // 开启一个事务
    [CATransaction begin];
    // 取消隐式动画 NO:不取消,YES:取消
    [CATransaction setDisableActions:YES];
    // 动画时间
    [CATransaction setAnimationDuration:2.0];
    
    self.layer.position = self.view.center;
    self.layer.cornerRadius = 50;
    // 提交事务
    [CATransaction commit];
}


@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容