Thursday, February 21, 2019

Create wait time in Swift

We want to wait a certain time, let use NSTimer.
Declare a count variable above viewDidLoad
var count=1
Copy this function in to above last close bracket.
func update(){
        count = count + 1
    if count==10{
            let vc = second()
            self.presentViewController(vc, animated: true, completion: nil)        }
    }
Add this line to inside viewDidLoad.
let time=NSTimer.scheduledTimerWithTimeInterval(1.0, target:self,  selector: "update",userInfo: nil, repeats: true)
We wait 10 seconds and open class second.

Another way is to create a delay function.
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
            ),
dispatch_get_main_queue(), closure)
    }

When want to wait 10 seconds, call function like this.
delay(10) {
let vc = third()
self.presentViewController(vc, animated: true, completion: nil)

  }

No comments:

Post a Comment