da1lbi3
da1lbi3

Reputation: 4519

Add a subview after a few seconds

I have this code:

 UIView.animateWithDuration(0.6, delay: 5.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 3.0, options: nil, animations: {

     self.view.addSubview(self.label!)

},completion: nil)

But it doesn't look like that the label is placed after 5.0 seconds. In the app you see it immediately.

How can i fix the problem that the label will be placed after 5 seconds?

Upvotes: 0

Views: 467

Answers (1)

ielyamani
ielyamani

Reputation: 18591

Bear in mind that with animateWithDuration() - (in the MVC pattern) - the View animation takes time to execute but the Model changes immediately and not after the given duration.

Use this function

func delay(delay:Double, closure:()->()) {
        dispatch_after(
            dispatch_time(
                DISPATCH_TIME_NOW,
                Int64(delay * Double(NSEC_PER_SEC))
            ),
            dispatch_get_main_queue(), closure)
}

for example to add a subview after a certain delay:

delay(5.0, closure: {self.view.addSubview(self.label!)})

Upvotes: 2

Related Questions