如何从方法内显示 UIActivityIndi​​catorView 并开始为其设置动画

发布于 2024-09-11 06:26:20 字数 394 浏览 5 评论 0原文

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

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

activityIndicator.hidden = NO;
[activityIndicator startAnimating];

在方法的开头

写:但当然它不会工作。 UIActivityIndi​​catorView 仅在方法结束后才会显示动画。

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

有人知道该怎么做吗?

也许是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有你 2024-09-18 06:26:20

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

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

...当方法(aSelector)完成时,调用:

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

...然后您将停止活动指示器。

切勿从非主线程中调用任何 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:

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

...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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文