CATransaction 隐式动画的关闭

1,621 阅读1分钟

CALayer有一个隐式动画:

首先在控制器的.m文件中:

- (void)viewDidLoad {

[superviewDidLoad];

CALayer* layer = [CALayerlayer];

layer.backgroundColor=UIColor.redColor.CGColor;

layer.position=CGPointMake(0,0);

layer.bounds=CGRectMake(0,0,100,100);

[self.view.layeraddSublayer:layer];

self.layer= layer;

}

写了上述的代码,创建一个layer并且添加到控制器的View上面。

在控制器的.m文件中添加如下的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event {

UITouch* touch = touches.anyObject;

CGPointp = [touchlocationInView:touch.view];

self.layer.position= p;

self.layer.backgroundColor=self.getRandomColor.CGColor;

}

这样会的话,点击屏幕,这个layer就会有一个动画效果,这个是隐式动画,因为我们并没有对它开启一个动画

不过我们可以通过添加一些代码来对这个隐式动画进行关闭。

[CATransactionsetDisableActions:YES];

self.layer.position= p;

self.layer.backgroundColor=self.getRandomColor.CGColor;

[CATransactioncommit];

将第二段代码添上两行代码,就可以关闭这个隐式动画效果。