UIActivityIndicator是否需要在iPhone上进行手动线程处理 [英] Does UIActivityIndicator require manual threading on iPhone
问题描述
我正在运行创建一个执行昂贵操作的iPhone应用程序,我想创建一个activityIndicator让用户知道应用程序没有被冻结。
I am running creating an iPhone application which performs a costly operation and I wanted to create an activityIndicator to let the user know the application has not frozen.
操作完全在一个事件调用中执行...所以UI框架没有机会接收控件来实际显示和动画这个指标。
The operation is performed entirely in one event call... so there is no chance for the UI framework to receive control to actually display and animate this indicator.
使用的示例应用程序UIActivityIndicator(或任何其他类似的动画)在不同的事件中启动和停止动画,在程序的不同阶段单独触发。
The sample apps which use the UIActivityIndicator (or any other similar animation) start and stop the animation in different events, triggered separately at different stages of the program.
我是否需要手动创建一个单独的线程运行我的操作,或者是否已经默认支持这种行为?
Do I need to manually create a separate thread to run my operation in, or is there already default support for this kind of behavior?
推荐答案
我刚发现有人问非常相似的问题,在苹果iPhone论坛上有一个很好的答案。
https://devforums.apple.com/message/24220#24220#注意:你必须有一个appleID来查看它。
I just found someone asking a very similar question with a good answer on the apple iPhone forums. https://devforums.apple.com/message/24220#24220 #Note: you have to have an appleID to view it.
基本上,是的,你需要开始在不同的线程中运行你的进程,但它很容易要做(从用户'eskimo1'转述)
Essentially, yes, you do need to start your process running in a different thread, but its quite easy to do (as paraphrased from user 'eskimo1')
- (IBAction)syncOnThreadAction:(id)sender
{
[self willStartJob];
id myObject = [MyObjectClass createNewObject];
[self performSelectorInBackground:
@selector(inThreadStartDoJob:)
withObject:myObject
];
}
- (void)inThreadStartDoJob:(id)theJobToDo
{
NSAutoreleasePool * pool;
NSString * status;
pool = [[NSAutoreleasePool alloc] init];
assert(pool != nil);
status = [theJobToDo countGrainsOfSandOnBeach];
[self performSelectorOnMainThread:
@selector(didStopJobWithStatus:)
withObject:status
waitUntilDone:NO
];
[pool drain];
}
其中 -willStartJob
和 -didStopJobWithStatus
是我自己的方法:
where -willStartJob
and -didStopJobWithStatus
are my own methods that:
- 禁用UI,以防止用户一次开始两个工作
- 设置活动指标
这篇关于UIActivityIndicator是否需要在iPhone上进行手动线程处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!