UIActivityIndicatorView 在 dispatch_sync 模式下不工作
UIActivityIndicatorView does not work in the mode dispatch_sync
我在我的项目中使用了UIActivityIndicatorView
,但是它不起作用,下面是我的代码:
let globalQueueDefault = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
dispatch_sync(globalQueueDefault){
self.activityIndicatorView.hidden = false
self.activityIndicatorView.startAnimating()
self.connect()
sleep(6)
dispatch_sync(globalQueueDefault) { () -> Void in
self.activityIndicatorView.stopAnimating()
self.activityIndicatorView.hidden = true
}
}
我已经尝试了 dispatch_sync
和 dispatch_async
...但是没有用。
使用此队列时,您要求代码在后台 运行,UI 代码只能在主线程上 运行。
将您的 activityView 包装在
中
dispatch_async(dispatch_get_main_queue(), {() -> Void in
})
将把该代码放回主线程,因为它应该按预期工作。
修改后的代码应该类似于
让 globalQueueDefault = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.activityIndicatorView.hidden = false
self.activityIndicatorView.startAnimating()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { [weak self] () -> Void in
self?.connect()
sleep(6)
})
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.activityIndicatorView.stopAnimating()
self.activityIndicatorView.hidden = true
}
}
UIKit一般都是从主线程处理。因此,每当您尝试更新 UI 时,您都必须将 UI 相关代码放入主线程中,如下所示。
dispatch_async(dispatch_get_main_queue(), ^{
// All UI related code goes here...
// I am adding few part of your code here.. Please update this according to your requirement.
self.activityIndicatorView.stopAnimating()
self.activityIndicatorView.hidden = true
});
我在我的项目中使用了UIActivityIndicatorView
,但是它不起作用,下面是我的代码:
let globalQueueDefault = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
dispatch_sync(globalQueueDefault){
self.activityIndicatorView.hidden = false
self.activityIndicatorView.startAnimating()
self.connect()
sleep(6)
dispatch_sync(globalQueueDefault) { () -> Void in
self.activityIndicatorView.stopAnimating()
self.activityIndicatorView.hidden = true
}
}
我已经尝试了 dispatch_sync
和 dispatch_async
...但是没有用。
使用此队列时,您要求代码在后台 运行,UI 代码只能在主线程上 运行。
将您的 activityView 包装在
中dispatch_async(dispatch_get_main_queue(), {() -> Void in
})
将把该代码放回主线程,因为它应该按预期工作。
修改后的代码应该类似于
让 globalQueueDefault = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.activityIndicatorView.hidden = false
self.activityIndicatorView.startAnimating()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { [weak self] () -> Void in
self?.connect()
sleep(6)
})
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.activityIndicatorView.stopAnimating()
self.activityIndicatorView.hidden = true
}
}
UIKit一般都是从主线程处理。因此,每当您尝试更新 UI 时,您都必须将 UI 相关代码放入主线程中,如下所示。
dispatch_async(dispatch_get_main_queue(), ^{
// All UI related code goes here...
// I am adding few part of your code here.. Please update this according to your requirement.
self.activityIndicatorView.stopAnimating()
self.activityIndicatorView.hidden = true
});