每個自定義的layer都存在默認的隱私動畫,隱式動畫默認爲1/4秒dom
@interface DYViewController () @property (nonatomic, weak) CALayer *layer; @end @implementation DYViewController - (void)viewDidLoad { [super viewDidLoad]; CALayer *layer = [CALayer layer]; // 設置尺寸 layer.bounds = CGRectMake(0, 0, 100, 100); // 顏色 layer.backgroundColor = [UIColor redColor].CGColor; [self.view.layer addSublayer:layer]; _layer = layer; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 獲取觸摸點 UITouch *touch = [touches anyObject]; CGPoint pos = [touch locationInView:self.view]; // 開啓事務 // [CATransaction begin]; // // // 取消隱世動畫 // [CATransaction setDisableActions:NO]; // 設置邊框 _layer.borderWidth = arc4random_uniform(5) + 1; CGFloat r = arc4random_uniform(256) / 255.0; CGFloat g = arc4random_uniform(256) / 255.0; CGFloat b = arc4random_uniform(256) / 255.0; // 設置背景顏色 _layer.backgroundColor = [UIColor colorWithRed:r green:g blue:b alpha:1].CGColor; // 設置圓角半徑 _layer.cornerRadius = arc4random_uniform(50); // 設置位置 _layer.position = pos; // // 提交事務 // [CATransaction commit]; }
若是關閉默認的動畫效果:能夠經過動畫的事務方法實現。動畫
// 開啓事務 // [CATransaction begin]; // // // 取消隱世動畫 // [CATransaction setDisableActions:NO]; // // 提交事務 // [CATransaction commit];