大家好,如果我有这样的东西:
my code....
// active indicator activity
[otherClass method]; // method that takes 5-6 seconds
// disable indicator activity
my code...当调用long方法时,我的类中的代码被阻塞了,对吗?
如果我在调用方法之前激活了一个指示器活动,当" method“正在执行时,它会变成动画吗?
谢谢。
发布于 2011-05-13 15:21:19
正如iceydee提到的,UI元素(如您的活动指示器)在主线程上运行。如果你加载一个大文件,下载一些东西或任何其他需要时间的东西,如果你想要动画UI元素,你必须在其他线程上执行。您可以使用中央调度中心,performSelectorInBackGround或其他技术(不推荐)。我会说:
my code....
// active indicator activity
[otherClass performSelectorInBackground:@selector(method) withObject:nil]; // method that takes 5-6 seconds
my code...然后在otherClass的方法中,停止主线程上的活动指示器:
[activityIndicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];发布于 2011-05-13 15:08:12
您应该避免阻塞主线程那么长时间,请考虑将该方法拆分为两个单独线程中运行的otherClass方法。主线程用于UI更新,不确定指示器是否能够在主线程被阻塞的情况下运行,我认为不能。
发布于 2011-05-13 15:11:37
是的,它将被阻塞,除非你在另一个线程中运行你的long方法。
要做到这一点,请使用this之类的技术。请参阅performSelectorInBackground和performSelectorOnMainThread.
https://stackoverflow.com/questions/5988391
复制相似问题