如何从方法内显示 UIActivityIndicatorView 并开始为其设置动画
我有一个执行耗时操作的方法,例如连续十次调用 [[NSString alloc] initWithContentsOfURL:u];
我想要一个在方法调用之前处于隐藏状态的 UIActivityIndicatorView 来显示和动画,所以我
activityIndicator.hidden = NO;
[activityIndicator startAnimating];
在方法的开头
写:但当然它不会工作。 UIActivityIndicatorView 仅在方法结束后才会显示动画。
这是不可接受的。我必须在函数调用期间显示动画。
有人知道该怎么做吗?
也许是 NSOperation? (有人有样本吗?)
I have a method that does a time consuming operation, say something like ten consecutive calls to
[[NSString alloc] initWithContentsOfURL:u];
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
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.
Anyone knows how to do it?
NSOperation maybe? (anyone has a sample thereof?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您正在此方法中执行一些昂贵的工作,并且在执行该工作时,您希望活动指示器旋转。昂贵的工作不应该在主线程上完成(iOS 可能会杀死你的应用程序!)。将您昂贵的工作放在一个单独的线程上:
...当方法(aSelector)完成时,调用:
...然后您将停止活动指示器。
切勿从非主线程中调用任何 UI 代码!
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:
...and when the method (aSelector) is done, call:
...and there you stop the activity indicator.
Never call any UI code from within a non-main thread!