2015-10-20 98 views
1

我想实现一个UIActivityIndi​​catorView,当用户在应用程序内购买时运行。出于某种原因,即使我已经制作了视图的子视图,UIActivityIndi​​catorView也没有显示出来。UIActivityIndi​​catorView没有显示

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() 
     }) 
}) 
+0

查看所有的右边所示的相关问题 - >你应该检查那些。这个问题之前已经被询问和回答。 – rmaddy

+1

@rmaddy我已经检查了他们,答案并没有解决我的问题,这就是为什么我发布我自己的问题。 – wxcoder

回答

1

我们再看一下后,这个问题很简单。您很快就会停止并删除活动指示器。您需要停止并在完成块中将其删除。

@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) 
     } 
    }) 
} 

您还需要确保完成块的内容在主线程上完成。

+0

这确实得到活动指示器显示,但在交易完成后,UIActivityIndi​​catorView仍然在旋转,因为没有错误 – wxcoder

+0

您是否像我在答案中那样制作代码? – rmaddy

+0

我把它放在错误的地方,我的错误。谢谢。 – wxcoder

0

问题是你做这个

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) 
      } 
     }) 
相关问题