Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Looking to delay segue between view controllers when button is pressed

I am fairly new to swift and Objective C, and I have an app that goes from the main view controller to the second view controller on a button press. THe second view controller opens up a camera and every time I click the button there is somewhat of a lag in the camera opening. I reckon this has something to do with the thread and how the process is being handled.

I want to introduce a delay between when my button on the first view controller is placed and when the second viewcontroller is displayed on the screen. Is there a delay that can be applied this way?

Here is the code to my main view controller with the button:

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.
    }

}

Thank you!

like image 622
Veejay Avatar asked Dec 19 '22 05:12

Veejay


1 Answers

Whenever you user performSegue method then you need to use like that

In the example with a 10 second delay

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

Updated

In this example with 5 second delay

DispatchQueue.main.asyncAfter(deadline:.now() + 5.0, execute: {
   self.performSegue(withIdentifier:"segue",sender: self)
})
like image 175
Pushpendra Avatar answered May 22 '23 03:05

Pushpendra