Nazim Kerimbekov
Nazim Kerimbekov

Reputation: 4783

Changing an image with a delay

for the past couple of hours I was trying to change an image in Xcode with a delay. I have tried achieving this using the following code:

UIImageView.animate(withDuration: 1, delay: 2, options: [], animations: {
    self.TapTap_intro.image = UIImage(named: "Second TapTap")
}, completion: nil)}

The problem while using this code is that it appears to not respect the delay and change the image immediately.

Could anyone please explain me what am I doing wrong and how could I possibly fix this issue?

Upvotes: 0

Views: 710

Answers (3)

Ajay saini
Ajay saini

Reputation: 2460

Use a timer instead of animate:

let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(changeImage), userInfo: nil, repeats: false)

And create a function to change the image:

func changeImage() {
   self.TapTap_intro.image = UIImage(named: "Second TapTap")
}

Upvotes: 0

Glaphi
Glaphi

Reputation: 452

DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
    // Do your thing         
}

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100503

To make a change without animation , there is no need to use UIView.animate You can dispatch it after some delay

 DispatchQueue.main.asyncAfter(deadline: .now() + 2 ) {
     self.TapTap_intro.image = UIImage(named: "Second TapTap")
 }

Upvotes: 1

Related Questions