从按钮动作调用时,UIActivityIndicatorView不显示 [英] UIActivityIndicatorView not showing up when invoked from button action
问题描述
我正在尝试通过按钮按压调用来显示一个微调器,但是我无法显示它.这是我的代码:
I'm trying to show a spinner during a possibly lengthy action invoked by a button press, but I can't get it to show up. Here's my code:
class ViewController: UIViewController {
var actInd : UIActivityIndicatorView!
[...]
override func viewDidLoad() {
super.viewDidLoad()
self.actInd = UIActivityIndicatorView(frame: CGRectMake(0,0, 50, 50)) as UIActivityIndicatorView
self.actInd.center = view.center
self.actInd.hidesWhenStopped = true
self.actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
self.view.addSubview(actInd)
[...]
}
@IBAction func buttonPressed(sender: UIBarButtonItem) {
self.actInd.startAnimating()
// [...] do some work
self.actInd.stopAnimating()
}
}
在我看来,当从执行按下按钮的UI线程中启动时,动画没有显示(如果我仅在不停止的情况下在按钮调用中启动动画,则它在buttonPressed回调后立即开始显示完成).我试着玩dispatch_async
,但没有得到任何结果.这是我尝试过的:
It looks to me like the animation doesn't get shown when started from within the UI thread executing the button pressed (if I only start the animation in the button call without stopping, it starts to display as soon as the buttonPressed callback finishes). I tried to play around with dispatch_async
but didn't get anywhere. Here's what I tried:
第一:
@IBAction func buttonPressed(sender: UIBarButtonItem) {
dispatch_async(dispatch_get_main_queue()) {
self.actInd.startAnimating()
}
shufflePlaylist()
dispatch_async(dispatch_get_main_queue()) {
self.actInd.stopAnimating()
}
}
第二:
@IBAction func buttonPressed(sender: UIBarButtonItem) {
dispatch_async(dispatch_get_main_queue()) {
shufflePlaylist()
}
}
func shufflePlaylist() {
self.actInd.startAnimating()
// [...] do the work
self.actInd.stopAnimating()
}
第三:
@IBAction func buttonPressed(sender: UIBarButtonItem) {
self.actInd.startAnimating()
dispatch_async(dispatch_get_main_queue()) {
shufflePlaylist()
}
}
func shufflePlaylist() {
// [...] do the work
self.actInd.stopAnimating()
}
在用户删除commitEditingStyle
中的表视图行(这也会触发一些冗长的数据更新操作)之后,尝试显示微调器时,我遇到了同样的问题.
I run into the same problem when trying to show the spinner after the user deletes a table view row in (which also can trigger some lengthy data update operation) in commitEditingStyle
.
那么在这些情况下显示活动指标的正确方法是什么?
So what's the right approach to show an activity indicator in those cases?
推荐答案
您想要的东西是:
@IBAction func buttonPressed(sender: UIBarButtonItem) {
spinner.startAnimating()
let dispatchPriority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(dispatchPriority, 0)) {
NSThread.sleepForTimeInterval(5.0) //your task here instead of this line
dispatch_async(dispatch_get_main_queue(), {
self.spinner.stopAnimating()
})
}
}
这将启动微调器,然后将其分派到后台线程,在其中执行任务(我假设您正在执行的任务是可以在后台线程上执行的任务-即它不涉及UI东西).任务完成后,该方法将分派回主UI线程,并停止微调器.
This starts the spinner,then dispatches to a background thread, performs a task there (I'm assuming the task you are performing is one that can be performed on a background thread - i.e it doesn't involve UI stuff). Once the task is complete, the method dispatches back to the main UI thread, and stops the spinner.
这篇关于从按钮动作调用时,UIActivityIndicatorView不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!