如何在方法中显示和开始设置UIActivityIndi​​catorView的动画 [英] How do I show and start animating a UIActivityIndicatorView from within a method

查看:100
本文介绍了如何在方法中显示和开始设置UIActivityIndi​​catorView的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行耗时操作的方法,例如连续十次调用
[[NSString alloc] initWithContentsOfURL:u];

I have a method that does a time consuming operation, say something like ten consecutive calls to [[NSString alloc] initWithContentsOfURL:u];

我想要一个在显示和动画的方法调用之前处于隐藏状态的UIActivityIndi​​catorView,所以我写道:

I want a UIActivityIndicatorView that was in a hidden state before the method call to show and animate, so I write:

activityIndicator.hidden = NO;
[activityIndicator startAnimating];

方法开头的

at the beginning of the method

但当然不行。只有方法结束后,UIActivityIndi​​catorView才会生成动画。

but of course it won't work. The UIActivityIndicatorView will only animate once the method is over.

这是不可接受的。我必须在函数调用期间显示动画。

This is not acceptable. I must show the animation during the function call.

任何人都知道怎么做?

NSOperation可能吗? (任何人都有一个样本吗?)

NSOperation maybe? (anyone has a sample thereof?)

推荐答案

我假设你在这个方法上做了一些昂贵的工作,而这项工作正在进行中执行后,您希望活动指示器旋转。昂贵的工作应该在主线程上完成 NOT (iOS可能会杀死你的应用程序!)。将您的昂贵工作放在一个单独的线程上:

I assume you are doing some expensive work in this method and while that work is being performed, you want the activity indicator to spin. Expensive work should NOT be done on the main thread (iOS might kill your app!). Put your expensive work on a separate thread with:

- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg

...当方法(aSelector)完成后,请致电:

...and when the method (aSelector) is done, call:

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait 

...然后你停止活动指示器。

...and there you stop the activity indicator.

永远不要调用任何UI代码来自非主线程!

Never call any UI code from within a non-main thread!

这篇关于如何在方法中显示和开始设置UIActivityIndi​​catorView的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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