单击按钮时无法播放图像序列

时间:2019-03-02 16:47:32

标签: ios swift iphone

我试图通过单击下一步按钮来播放动画图像的分离图像。图像名称以字符串形式存储在数组中。当我单击下一步按钮时,图像转换是第一次发生。下次,这不会发生。当我尝试打印i的值时,要检查单击下一个按钮时发生了什么,该值没有增加。下面是我的代码:

    import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var image2: UIImageView!
    // @IBOutlet weak var imageView1: UIImageView!

  //        img1.image=UIImage(named:"frame2.gif")

    @IBAction func next(_ sender: Any) {
        var imgarray=["frame2.gif","frame3.gif","frame4.gif","frame5.gif","frame6.gif"]
        var i=0
        if i<7{
            image2.image=UIImage(named:imgarray[i])
            i+=1
        }
        else if i>=7{
            i=0
        }
               print(i)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }


}

1 个答案:

答案 0 :(得分:1)

var i=0放在按钮操作之外。否则,当您单击按钮时,您的i将始终以0

重新初始化
import UIKit

class ViewController: UIViewController {

var i=0
    @IBOutlet weak var image2: UIImageView!
    // @IBOutlet weak var imageView1: UIImageView!

  //        img1.image=UIImage(named:"frame2.gif")

    @IBAction func next(_ sender: Any) {
        var imgarray=["frame2.gif","frame3.gif","frame4.gif","frame5.gif","frame6.gif"]

        if i<7{
            image2.image=UIImage(named:imgarray[i])
            i+=1
        }
        else if i>=7{
            i=0
        }
               print(i)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }


}