UIActivityIndi​​catorView没有显示出来

时间:2015-10-20 22:12:48

标签: ios swift uiactivityindicatorview

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

  EXPORTABLE VARIABLES
        $Bin         - path to bin directory from where script was invoked
        $Script      - basename of script from which Perl was invoked
        $RealBin     - $Bin with all links resolved
        $RealScript  - $Script with all links resolved

PFRestore:

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

2 个答案:

答案 0 :(得分: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)
        }
    })
}

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

答案 1 :(得分: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)
            }
        })