禁用隐式动画而不执行 CATransaction 开始和提交



我看到很多人用这个来禁用隐式动画:

[CATransaction begin];
[CATransaction setDisableActions:YES];
someLayer.backgroundColor = someCGColor;//no animation
[CATransaction commit];

但是如果没有cattransaction begin&commit ,它也可以工作:

[CATransaction setDisableActions:YES];
someLayer.backgroundColor = someCGColor;//no animation

像这样也可以:

[CATransaction setDisableActions:YES];
someLayer1.backgroundColor = someCGColor;//no animation
[CATransaction setDisableActions:NO];
someLayer2.backgroundColor = someCGColor2; //have animation
所以问题是,为什么我需要使用 cattransactionbegin&commit?在某些情况下我必须使用它们吗?

谢谢你,阿姨。

这与Core Animation中的事务块有关。默认情况下,有一个隐式事务块自动捕获对cattransaction的调用。使用CATransaction begin/commit创建一个显式事务块,它允许你对动画的不同元素应用不同的动画特性。

理论上,如果某些事情需要立即完成,而不是在下一次重绘调用时完成,例如添加或删除动画,则可能需要显式事务块。如果操作不当,这会导致问题,例如在任何绘制调用完成之前启动动画。

最新更新