4

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!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Veejay
  • 397
  • 3
  • 10
  • 17
  • I think you should probably be looking at fixing the delay. Can you show us how you are adding the camera layer ? I have added that in viewDidAppear and works fine – GoodSp33d Aug 08 '17 at 07:33
  • I am using the first screen to connect to this open source code here. The camera initialization takes place in this viewcontroller here: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/ios/camera/CameraExampleViewController.mm – Veejay Aug 08 '17 at 07:41
  • Refer this: https://developer.apple.com/documentation/objectivec/nsobject#//apple_ref/occ/instm/NSObject/performSelector:withObject:afterDelay: – dmaulikr Aug 08 '17 at 08:56

2 Answers2

11

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)
})
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Pushpendra
  • 1,492
  • 1
  • 17
  • 33
  • Thank you for that answer, unfortunately adding this into my code does not cause the 10 second delay at all, I must be doing something incorrectly. I have the code as follows and expect a 10 second delay after I press the items button. @IBAction func itemAction(_ sender: AnyObject) { DispatchQueue.main.asyncAfter(deadline:.now() + 5.0, execute: { self.performSegue(withIdentifier:"segue",sender: self) }) } Am i missing something here? – Veejay Aug 08 '17 at 07:55
  • I do not think that you was missing something in that. i might be happened because of `cleaning code` or `project`. But at all if it's working fine you then please up vote my answer so that some one can take help of this. I also added your code in that check updated Thanks – Pushpendra Aug 08 '17 at 08:21
  • 2
    @Viv You would have probably set the segue in storyboard for the button then. Add a manual segue from view controller for this delay to work – GoodSp33d Aug 08 '17 at 10:11
  • @GoodSp33d YES! That was it! – Veejay Aug 08 '17 at 22:34
  • 1
    @APKAPPS Thank you for your answer! Definitely put me on the right track :) – Veejay Aug 08 '17 at 22:35
1
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 )
}
Phyber
  • 1,368
  • 11
  • 25
  • thank you! I tried implementing this as follows and there was no delay after the button press at all. I'm definitely doing something incorrectly, not sure what though: @IBAction func itemAction(_ sender: AnyObject) { let timer = Timer.scheduledTimer(withTimeInterval: 10.0, repeats: false) { (timer) in self.performSegue(withIdentifier: "segue", sender: self ) } } – Veejay Aug 08 '17 at 08:18
  • @Viv below link may be useful to you https://stackoverflow.com/a/39845573 – Rajesh Dharani Aug 08 '17 at 08:34