如何在viewDidLoad中为performSegue添加延迟?

时间:2017-11-24 11:51:39

标签: ios swift swift4

在我的第一个viewController的viewDidLoad中,如果用户已通过身份验证 使用performSegue

自动发送到家中
override func viewDidLoad() {
    super.viewDidLoad()


    Auth.auth().addStateDidChangeListener() { (auth, user) in
        if user != nil{

            self.ballClipRotatePulse.startAnimating()
            self.performSegue(withIdentifier: "goToHome1", sender: self)

        }
    }
}

但是因为我希望用户首先看到加载动画,我怎么能这样做才能比self.ballClipRotatePulse.startAnimating()晚两秒开始执行performSegue?

2 个答案:

答案 0 :(得分:1)

语法很简单:

Auth.auth().addStateDidChangeListener() { (auth, user) in
    if user != nil {
        self.ballClipRotatePulse.startAnimating()
        // to run something in 2 seconds
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
            // your code here
            self.performSegue(withIdentifier: "goToHome1", sender: self)
        }
    }
}

答案 1 :(得分:0)

试试这个

let when = DispatchTime.now() + 2.0 // 2 is desirable delay
DispatchQueue.main.asyncAfter(deadline: when){ // your postponed code }

顺便说一句,不要忘记用户在延迟期间可能会触摸某些界面元素,因此您的应用可能会出现意外行为。更好的解决方案是从动画完成处理程序中调用performSegue