iOS按下按钮时,希望延迟视图控制器之间的segue

时间:2017-08-08 07:29:54

标签: ios swift uiviewcontroller

我对swift和Objective C相当新,我有一个应用程序,从按下按钮的主视图控制器到第二个视图控制器。第二个视图控制器打开一个摄像头,每次我点击按钮时,摄像头打开都会有一些延迟。我认为这与线程以及如何处理进程有关。

我想在我放置第一个视图控制器上的按钮和屏幕上显示第二个视图控制器之间引入延迟。是否存在可以这种方式应用的延迟?

以下是带有按钮的主视图控制器的代码:

import UIKit

class ViewController: UIViewController {

    @IBAction func itemAction(_ sender: AnyObject) {
        performSegue(withIdentifier: "segue", sender: self )
    }

    @IBAction func logosAction(_ sender: Any) {
        performSegue(withIdentifier: "segue2", sender: self )
    }

    @IBOutlet var itemsButton: UIButton!

    @IBOutlet var carsButton: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()

        itemsButton.layer.cornerRadius = 10
        itemsButton.clipsToBounds = true

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

谢谢!

2 个答案:

答案 0 :(得分:8)

每当您使用performSegue方法时,您都需要使用

  

在延迟10秒的示例中

DispatchQueue.main.asyncAfter(deadline: .now() + 10.0, execute: {
   self.performSegue(withIdentifier: "segue", sender: self )
})

<强>更新

  

在此示例中有5秒延迟

DispatchQueue.main.asyncAfter(deadline:.now() + 5.0, execute: {
   self.performSegue(withIdentifier:"segue",sender: self)
})
  

希望这会对你有所帮助。

答案 1 :(得分:0)

DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
    self.performSegue(withIdentifier: "segue", sender: self )
}

OR

let timer = Timer.scheduledTimer(withTimeInterval: 10.0, repeats: false) { (timer) in
    self.performSegue(withIdentifier: "segue", sender: self )
}