嵌套 CATransaction 的行为?

Behave of nested CATransaction?

下面的代码将在 commit 之后立即更新 backgroundColor

[CATransaction begin];
self.view.backgroundColor = [UIColor redColor];
[CATransaction commit];

sleep(5);

但是对于嵌套显式 CATransactions,屏幕仅在最外层事务提交时更新。

[CATransaction begin];
[CATransaction begin];
self.view.backgroundColor = [UIColor redColor];
[CATransaction commit];
sleep(5);
[CATransaction commit];

所以这很奇怪,因为我们知道runloop会在每个循环中创建一个最外层的隐式事务。为什么这个 implicit transactionexplicit transaction 提交时不被视为最外层事务?

总是有隐式交易。也可以有显式事务。在您的所有代码完成 运行 之前,隐式事务不会提交。如果您有显式事务(begincommit),那么它会在遇到 commit 时提交。

嵌套显式事务的目的只是为了让您可以为动画的不同部分提供不同的参数(例如持续时间);实际的提交直到最外层 commit 才会发生。阅读 the docs:

Only after you commit the changes for the outermost transaction does Core Animation begin the associated animations