2016-06-30 110 views
0

我有一个UIActivityIndi​​catorView运行良好。但我的问题是,如何防止应用程序用户通过应用程序导航,或者在显示UIActivityIndi​​cator时单击按钮?我想强制用户等到我运行的代码完成。这是我到目前为止。任何帮助将不胜感激。swift UIActivityIndi​​catorView advice

class ViewController: UIViewController { 
    var activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge) 

var timer = NSTimer() 
    let delay = 4.5 

override func viewDidLoad() { 
    super.viewDidLoad() 

    view.addSubview(activityIndicator) 
    activityIndicator.translatesAutoresizingMaskIntoConstraints = false 
    activityIndicator.color = UIColor.redColor() 


    let horizontalConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0) 
    view.addConstraint(horizontalConstraint) 

    let verticalConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0) 
    view.addConstraint(verticalConstraint) 
getBalance() 
} 

func delayedAction(){ 
     // time consuming code here that will run in the background with task 

dispatch_async(dispatch_get_main_queue(), {() -> Void in 
         self.lblAccountBalance.text = "Account Balance " + myResult! 
         self.activityIndicator.stopAnimating(); 
        }) 
    } 

func 
    getBalance() { 
     activityIndicator.startAnimating() 
     timer = NSTimer.scheduledTimerWithTimeInterval(delay, target: self, selector: #selector(delayedAction), userInfo: nil, repeats: false) 
     } 

回答

1

超级简单!

self.view.userInteractionEnabled =像您期望从视图控制器的视图假

1

简单地禁用用户的交互可能并不总是工作。如果该视图之外的任何按钮不在视图的层次结构中,那么它们将是可点击的。

你应该做的是暂时中止在应用层的用户交互:

UIApplication.shared.beginIgnoringInteractionEvents() 
UIApplication.shared.endIgnoringInteractionEvents() 
相关问题