依次排列 3 张图像以显示在图像视图上

sequence 3 images right after another to display on a image view

我的 swift 下面函数中的代码在图像视图扇形中显示 2 张图像。我下面的代码现在成功地做到了,但它只能显示 2 张图像。我想分别显示 a、b、cd 一秒钟,然后无限循环重复。

let image1 = UIImage(named: "a.png")
let image2 = UIImage(named: "b.png")
let image3 = UIImage(named: "cd.png")
  var fan = UIImageView()




@objc func alterImage() {
    fan.image = fan.image == image2 ? image1 : image2
    perform(#selector(alterImage), with: self, afterDelay: 1)
}

试试这个

@objc func alterImage() {
    fan.image = fan.image == image1 ? image2 : fan.image == image2 ? image3 : image1
    perform(#selector(alterImage), with: self, afterDelay: 1)
}

UIImageView has a property called animationImages exactly por this purpose, one to control its duration animationduration, and one called animationRepeatCount to control its repetition. You just need to set it to zero for an endless cycle. Once you have setup those properties you just need to call UIImageView's method startAnimating()


import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        let image1 = UIImage(named: "a")!
        let image2 = UIImage(named: "b")!
        let image3 = UIImage(named: "c")!
        imageView.animationImages = [image1, image2, image3]
        imageView.animationDuration = 1
        imageView.animationRepeatCount = 0
        imageView.startAnimating()
    }
}