如何以编程方式在视图中间创建活动指示器?
我正在使用一个在线解析提要的应用程序。当我单击刷新按钮时,需要一些时间来重新解析文件并显示其数据。当我单击刷新按钮时,我希望在视图中间有一个活动指示器。解析完成后,该指示器应该隐藏。
我正在使用这段代码,但它不起作用:
- (IBAction)refreshFeed:(id)sender
{
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:spinner];
[spinner startAnimating];
// parsing code code
[spinner release];
}
I am working with an app which parses an feed online. When I click the refresh button, it takes some time to re parse the file and show its data. I want an activity indicator in the middle of the view when I click refresh button. And when parsing is done, that indicator should hide.
I am using this code but it is not working:
- (IBAction)refreshFeed:(id)sender
{
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:spinner];
[spinner startAnimating];
// parsing code code
[spinner release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
UIActivityIndicator 通常需要放置在长进程(解析提要)之外的单独线程中才能显示。
如果您想将所有内容保留在同一个线程中,那么您需要给指示器时间来显示。 此 Stackoverflow 问题解决了代码中的延迟问题。
编辑:两年后,我认为任何时候你使用延迟来使某件事发生时你都可能做错了。以下是我现在执行此任务的方式:
A UIActivityIndicator generally needs to be placed into a separate thread from the long process (parsing feed) in order to appear.
If you want to keep everything in the same thread, then you need to give the indicator time to appear. This Stackoverflow question addresses placing a delay in the code.
EDIT: Two years later, I think that any time you are using a delay to make something happen you are probably doing it wrong. Here is how I would do this task now:
根据您上面所说的,程序在解析期间没有响应,这是一个问题。如果在解析数据时 GUI 冻结,则应将该操作移至辅助线程。这样您的 GUI 就能保持响应,并且您将能够看到活动指示器。
在主线程上,您应该有与此类似的内容,以便显示活动指示器:
辅助线程完成后,这是解析数据的线程,您应该在主线程上调用类似的内容来删除活动指标:
from what you are saying above the program is not responding during the parsing which is a problem. If the GUI freezes while you are parsing the data you should move that operation to a secondary thread. That way your GUI remains responsive and you will be able to see the activity indicator.
on the main thread you should have something similar to this in order to show the activity indicator:
after the secondary thread is finished, this is the thread where you parse the data, you should call on the main thread something like this to remove the activity indicator:
将活动指示器的中心更改为
Change the center of the activity indicator as
对于 Swift
在适当的位置停止 ActivityIndicator wirte
[activityIndicator stopAnimating]
For Swift
To stop activityIndicator wirte
[activityIndicator stopAnimating]
on appropriate place在开始任务之前添加此代码:
任务完成后添加:
Add this code before starting your task:
After completion of the task add:
在 Swift 中,这适用于我的应用程序
In Swift, this worked for my app
更好的方法是在 xib & 上添加指标根据您的要求使其隐藏/取消隐藏
Better approach would be you add indicatore on xib & make it hide/unhide basedon your requirement
要从 rootController 显示微调器,请尝试:
或
To show spinner from rootController try:
or
尝试这样
try like this
其他一些答案的问题是每次都会添加一个新的活动指示器,因此子视图开始堆积。这是一个仅使用单个
UIActivityIndicator
的完整示例。这是上面项目的完整代码:
The problem with some of the other answers is that a new activity indicator gets added every time so the subviews start piling up. Here is a complete example that only uses a single
UIActivityIndicator
.This is the entire code for the project above:
你可以写 swift 4
You can write swift 4
SWIFT 5
声明
ViewDidLoad
Activate activity indicator, in my case it is in `didSelectRowAt` method
如果您在动画处于活动状态时推送到另一个视图控制器,然后弹出回来,您仍然会看到活动指示器动画,因此您应该这样做
SWIFT 5
Declaration
ViewDidLoad
Activate activity indicator, in my case it is in `didSelectRowAt` method
If you push to another viewController while animation is active, and then pop back, you will still see activity indicator animating, so you should do this
Poscean 先生我的代码正在运行。但是如果您想显示活动指示器的大外观,您可以使用它......
Mr Poscean my code is working. But is you want show big appearing of activity indicator you can use it....