带背景动画的 CATransiton 闪烁
我正在使用 CATransition
将 CALayer
进出 UIView
(UIView
不是全屏。)新图层从右侧进入,旧图层从左侧离开。 CAlayer
的内容属性设置为 CGImageRef
。这是我认为相关的代码:
CATransition *transition = [CATransition animation];
transition.duration = transitionTime;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
transition.delegate = self;
[view.layer addAnimation:transition forKey:nil];
// onDeck is the layer stage-right and is about to enter
[view.layer addSublayer:onDeck];
// onStage is the layer currently in the middle of the view and is exiting
if (onStage)
[onStage removeFromSuperlayer];
这个动画发生,有 1 秒的暂停,然后一个新的图像被动画化等等。这在模拟器中工作得很好,但在我的第二代 iPod Touch 上,有动画中偶尔会出现一些小问题。具体来说,动画层将在当前显示层的顶部闪烁。这些闪烁并不一致,但很明显。
我已经缩小到可能的罪魁祸首是我在这个转换背后进行的背景动画。背景中有几十个 CAlayer
在全屏视图中飞进飞出。当我删除这些背景动画或将过渡的动画持续时间设置为更高的持续时间(2 秒以上)时,动画效果很好(我希望动画时间为 0.75 秒。)
我的第一个想法(似乎是由上述观察结果支持)是我太努力地推动 CoreAnimation(至少对于我的 iPod)并且需要妥协。为什么我难以接受这一点,因为除了偶尔的闪烁之外,场景渲染得非常好(几乎没有延迟)。
如果有人对这个问题或一般的 CoreAnimation 优化有任何意见,我将非常感激!
感谢您阅读
更新:
有机会在 iPhone 4 上对此进行测试,并且从未发生过闪烁。此外,CoreAnimation 工具证实,在我的第二代 iPod Touch 上,我的 FPS 始终达到 40 秒高至 50 秒低/中。
I am using a CATransition
to shuffle CALayer
's in and out of a UIView
(the UIView
isn't fullscreen.) The new layer enters from the right and the old layer leaves to the left. The CAlayer
's have their contents properties set to CGImageRef
s. Here is what I believe to be the pertinent code:
CATransition *transition = [CATransition animation];
transition.duration = transitionTime;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
transition.delegate = self;
[view.layer addAnimation:transition forKey:nil];
// onDeck is the layer stage-right and is about to enter
[view.layer addSublayer:onDeck];
// onStage is the layer currently in the middle of the view and is exiting
if (onStage)
[onStage removeFromSuperlayer];
This animation occurs, there is a 1 second pause, and then a new image is animated in etc, etc. This works beautifully in the simulator but on my 2nd generation iPod Touch, there are occasional hiccups in the animation. Specifically, the layer being animated in will flicker on top of the currently displayed layer. These flickers are not consistent but are noticeable.
What I've narrowed down to be the likely culprit(s) are the background animations I have going on behind this transition. There are a few dozen CAlayer
's flying in and out of the full-screen view in the background. When I remove these background animations or set the animation duration for the transition to a much higher duration (2 seconds +) the animation performs fine (I'd like the animation time to be 0.75 seconds.)
My first thought (which seems to be backed up by the above observations) is that I am pushing CoreAnimation (at least for my iPod) too hard and need to compromise. Why I am having trouble accepting this is that the scene renders very nicely (little to no lag) aside from this occasional flicker.
If anyone has any input on this issue, or CoreAnimation optimization in general, I'd be very much appreciative!
Thanks for reading
Update:
Had a chance to test this on an iPhone 4 and the flickering never occurred. Additionally, CoreAnimation instrument confirmed that, on my 2nd generation iPod Touch, I am consistently getting high 40s to low/mid 50s in FPS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是猜测:您是否尝试过管理您的 CATransactions ?
您可以尝试将您对场景的修改括起来,如下所示:
Just a guess: have you tried managing your CATransactions at all?
You might try bracketing your modifications to the scene like this:
我会消除所有阴影(如果有的话),然后检查性能是否有所提高。我发现阴影,尤其是 CAShapeLayers 上的阴影,对动画的流畅度有很大影响。
I would get rid of all shadows (if you have any) and then check if performance improves. I've seen that shadows, particularly shadows on CAShapeLayers, have a great impact on the smoothness of animations.