如何阻止Core Animation阻止我的主线程? [英] How do I prevent Core Animation blocking my main thread?

查看:85
本文介绍了如何阻止Core Animation阻止我的主线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Core Animation在一个单独的线程中调度其动画,如文档中所述。然而,我的动画似乎阻止了我的主线程。所有动画开始和芬兰。 (有或没有kCATransactionDisableActions设置为true。)但是变得跳跃并且主要的runloop停止。

I'm aware of the fact that Core Animation dispatches its animations in a seperate thread, as stated in the documentation. Nevertheless, my animations seem to be blocking my main thread. All animations start and finnish. (With or without kCATransactionDisableActions set as true.) but become jumpy and the main runloop stalls.

我做错了什么?

概念示例:

[NSTimer scheduledTimerWithTimeInterval:0.0333  target:self selector:@selector(gameEngine) userInfo:nil repeats:YES];

- (void)gameEngine 
{    
   [CATransaction begin];
   [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
   myLayer.position = CGPointMake( newX, newY);
   [CATransaction commit];    
}


推荐答案

每秒创建30个新动画是一个非常糟糕的主意,而不是如何使用Core Animation。核心动画的设计理念是告诉系统你想要你的图层(或图层支持的视图)结束的位置,然后它会计算出其余部分,包括在你的时间范围内运行动画需要多少帧提供。动画本身将在后台线程上运行,但我相信有一些需要首先执行的设置,它发生在调用线程(通常是主线程)上。此设置可能需要超过1/30秒,这会使主线程超载。

Creating 30 new animations a second is a really bad idea, and not how Core Animation was intended to be used. Core Animation was designed around the idea that you tell the system where you want your layer (or layer-backed view) to end up, and it figures out the rest, including how many frames to drop to run the animation in the time frame you provide. The animations themselves will run on a background thread, but I believe there's a little setup that needs to be performed first which takes place on the calling thread (usually the main thread). This setup might take longer than 1/30th of a second, which would overload your main thread.

您的目标应该是最小化与Core Animation的交互量。如果您有一个复杂但脚本化的运动路径(或其他属性更改),请使用CAKeyframeAnimation提前设置它,并只调用该动画一次。如果对象的运动需要根据用户输入进行更改,那么只有在获得实际输入时才会这样做。更改CALayer中期动画的属性将使其平滑地从当前运动路径的中间移动到新目的地。

You goal should be to minimize the amount of interaction with Core Animation. If you have a complex, but scripted, motion path (or other property change), set it all up ahead of time using a CAKeyframeAnimation and just call that animation once. If there's something about the motion of your object that needs to be altered in response to user input, only do that when you get the actual input. Changing a property of a CALayer mid-animation will cause it to smoothly move from the middle of its current motion path to its new destination.

如果您使用Core Animation这种方式,您可以在iPhone上同时以60 FPS动画最多50个移动半透明层。

If you work with Core Animation in this fashion, you can simultaneously animate up to 50 moving translucent layers at 60 FPS on the iPhone.

这篇关于如何阻止Core Animation阻止我的主线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆