how to do imageView.startAnimating() with completion in swift? [duplicate]

拈花ヽ惹草 提交于 2019-12-24 14:12:38

问题


I would like a simple image array animation to run when a button is clicked. The below code works well, but I would like the last image in the array to be set as the imageview image after. How would I change the image on completion instead of it going back to the original?

let imageArray = [UIImage(named: "Frog-1.gif")!, UIImage(named: "Frog-2.gif")!, UIImage(named: "Frog-3.gif")!, UIImage(named: "Frog-4.gif")!]

@IBOutlet var imageView1: UIImageView!

@IBAction func button1(sender: UIButton) {

    imageView1.animationImages = imageArray
    imageView1.animationDuration = 1.0
    imageView1.animationRepeatCount = 1
    imageView1.startAnimating()

}

回答1:


Add #selector() and @objc at last solution and try:

When the button is pressed you can call a function with a delay

self.performSelector(#selector(afterAnimation), withObject: nil, afterDelay: imageView1.animationDuration)

Then stop the animation and add the last image of imageArray to the imageView in the afterAnimation function

@objc func afterAnimation() {
    imageView1.stopAnimating()
    imageView1.image = imageArray.last
}


来源:https://stackoverflow.com/questions/33740387/how-to-do-imageview-startanimating-with-completion-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!