如何快速完成imageView.startAnimating()? [英] how to do imageView.startAnimating() with completion in swift?

查看:204
本文介绍了如何快速完成imageView.startAnimating()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在单击按钮时运行一个简单的图像数组动画。下面的代码运行良好,但我希望将数组中的最后一个图像设置为之后的imageview图像。我将如何在完成时更改图像,而不是返回原始图像?

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()

}


推荐答案

在最后一个解决方案中添加#selector()和@objc并尝试:

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)

然后停止动画并将 imageArray 的最后一个图像添加到<$ c afterAnimation 函数中的$ c> imageView

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
}

这篇关于如何快速完成imageView.startAnimating()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆