CATransaction:实现翻页动画时出现的问题
我正在尝试实现一个翻页动画 - 当用户在页面中导航的速度不太快时,该动画就可以工作。
我的代码如下所示:
[CATransaction begin];
[CATransaction setAnimationDuration:duration];
[CATransaction setCompletionBlock:^{
if (pageDifference == 1 && setDelegate) {
[self cleanupFlip];
self.animationInProgress = NO;
}
}];
flipAnimationLayer.transform = endTransform;
[CATransaction commit];
endTransform
是一个 CATransform3DIdentity
,正如我所说,它工作得很好。在我的 cleanupFlip
中,我正在准备下一页的屏幕截图,这使我能够执行翻页动画。我正在制作的屏幕截图似乎是问题所在;一旦用户单击快速浏览页面,动画就不会发生,但页面只会发生变化 - 没有翻页动画。我通过设置一个标志 animationInProgress
得到了改进,它可以避免在旧动画完成之前开始下一个动画。
我不明白的是:我期望工作得这么好,因为我在动画的最后设置了 animationInProgress
标志,所以一切都应该为下一次运行做好准备 - 但是它不是......
在模拟器上我可以比在设备上更快地点击 - 在那里我必须等待一秒钟才能开始下一页翻页。
如果不进行屏幕截图(例如,仅使用空图像),一切都会按预期进行。如果 cleanupFlip
需要一些时间,这似乎是一个问题。
我有什么想法可以让这项工作成功吗?
多谢!
I'm trying to implement a page flipping animation - which works, when the user doesn't navigate too fast through the pages.
My code looks like this:
[CATransaction begin];
[CATransaction setAnimationDuration:duration];
[CATransaction setCompletionBlock:^{
if (pageDifference == 1 && setDelegate) {
[self cleanupFlip];
self.animationInProgress = NO;
}
}];
flipAnimationLayer.transform = endTransform;
[CATransaction commit];
The endTransform
is a CATransform3DIdentity
, which, as I said, works fine. In my cleanupFlip
I'm preparing the screenshot for the next page, which enables me to perform the page flipping animation. And the screenshot I'm makiing seems to be the problem; as soon as the user clicks to fast through the pages, the animation doesn't happen but the pages just change - without page flipping animation. I got an improvement through setting a flag animationInProgress
which avoids starting the next animation before the old finishes.
What I don't get: I'm expecting to work this fine, as I'm setting the animationInProgress
flag at the very end of the animation, so everything should be prepared for the next run - but it isn't...
On the simulator I can click faster than on the device - there I have to wait for a second until I can start the next page turn.
Without making the screenshot (e.g., just using an empty image), everything works as expected. It seems as if it's a problem if the cleanupFlip
takes some time.
Any ideas how I could make this work?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
造成这种行为的最可能原因是用户比动画更快。也就是说,第二个 [CATransaction begin] 在第一个动画完成之前启动,因此在执行 setCompletionBlock 之前启动。我不确定确切的顺序是什么,但是在代码中设置一些 NSLog 语句应该可以清楚地说明这一点。
一种解决方案可能是不在 setCompletionBlock 中创建图像,而是在开始之前创建图像。或者,如果创建图像需要花费大量时间,请在提交第一个事务后立即为下一页做好准备。
在 [CATransaction begin] 之前放置 [CATransactionlush] 也曾在此类情况下帮助过我。
The most likely cause of this behavior, is that the user is faster than the animation. That is, the second [CATransaction begin] is started before the first animation has finished, and therefore before your setCompletionBlock is executed. I am not sure what the exact order is, but setting some NSLog statements in your code should make it clear.
One solution might be to create the image not in the setCompletionBlock, but right before the begin. Or, if creation of the image takes a lot of time, prepare it for the next page right after the commit of the first transaction.
Putting a [CATransaction flush] right before the [CATransaction begin] has also helped me before in these type of situations.