我正在尝试实现一个在用户处于应用程序购买过程中运行的UIActivityIndicatorView。由于某些原因,即使我已经创建了视图的子视图,UIActivityIndicatorView也不会出现。
class RemoveAdsViewController: UIViewController {
@IBAction func btnAdRemoval(sender: UIButton) {
let buyProgress = UIActivityIndicatorView(activityIndicatorStyle: .White)
buyProgress.center = self.view.center
self.view.addSubview(buyProgress)
buyProgress.startAnimating()
print(buyProgress)
PFPurchase.buyProduct("", block: { (error:NSError?) -> Void in
if error != nil{
let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
})
buyProgress.stopAnimating()
buyProgress.removeFromSuperview()
}PFRestore:
restoreProgress.startAnimating()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
PFPurchase.restore()
dispatch_async(dispatch_get_main_queue(), {
restoreProgress.stopAnimating()
})
})发布于 2015-10-20 22:25:32
再看一看,问题很简单。停止并移除活动指示器太快了。您需要停止并在完成块中删除它。
@IBAction func btnAdRemoval(sender: UIButton) {
let buyProgress = UIActivityIndicatorView(activityIndicatorStyle: .White)
buyProgress.center = self.view.center
self.view.addSubview(buyProgress)
buyProgress.startAnimating()
print(buyProgress)
PFPurchase.buyProduct("", block: { (error:NSError?) -> Void in
buyProgress.stopAnimating()
buyProgress.removeFromSuperview()
if error != nil{
let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
})
}还需要确保完成块的内容是在主线程上完成的。
发布于 2015-10-20 22:28:24
问题是你这么做
buyProgress.startAnimating()紧随其后的是
buyProgress.stopAnimating()因为PFPurchase.buyProduct是一个异步调用,它将立即返回,而您没有看到您的活动指示符是在一个运行循环循环中发生的。
你得走了
buyProgress.stopAnimating()在封闭里面,就像这样
PFPurchase.buyProduct("", block: { (error:NSError?) -> Void in
if error != nil{
let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
buyProgress.stopAnimating()
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
})https://stackoverflow.com/questions/33247724
复制相似问题