使用结构显示一定时间的图像

use struct to display image for certain length of time

我希望下面的 swift 代码获取结构数据并在类似 for 循环的地方使用。循环应该采用 arrayOne 并在持续时间内使用它来确定图像应该在 imageview pic 上显示多长时间。 Uiimage 数组应该对应于 int.So 有 2 个图像和两个 int 只是使它们在 imageview pic 上显示与它们各自数组中匹配的 int。

    import UIKit

class ViewController: UIViewController {
    var pic = UIImageView()
    
    
    var somePics:[UIImage] = [ UIImage(named: "a.png")!,UIImage(named: "b.png")!]
    var emptyb = [UIImage]()
    var someInts:[Int] = [10, 20]

    

    override func viewDidLoad() {
        super.viewDidLoad()
       Added(arrayOne: someInts, arrayTwo: emptyb)
        view.addSubview(pic)
        pic.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
        pic.backgroundColor = .yellow
        
        //for added.

    }


}


struct Added {
    var arrayOne: [Int]
    var arrayTwo: [UIImage]
}

完整的例子。检查您是否一切正常!

class ViewController: UIViewController {
    var pic = UIImageView()
    var somePics: [UIImage] = [UIImage(named: "image1")!, UIImage(named: "image2")!]
    var emptyb = [UIImage]()
    var someInts:[Int] = [5, 6]
    var currentIndex = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        view.addSubview(pic)
        pic.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
        pic.backgroundColor = .yellow
        runGallery()
    }
    
    func runGallery() {
        guard !somePics.isEmpty else {
            return
        }
        currentIndex = 0
        newTimerForIndex(index: 0)
    }
    
    func newTimerForIndex(index: Int) {
        let image = somePics[index]
        pic.image = image
        Timer.scheduledTimer(withTimeInterval: Double(someInts[index]), repeats: false) { timer in
            if self.currentIndex + 1 < self.somePics.count {
                //RUN gallery for next image
                self.currentIndex += 1
                self.newTimerForIndex(index: self.currentIndex)
            } else {
                //STOP on last element
                self.pic.image = nil
            }
        }
    }
}


struct Added {
    var arrayOne: [Int]
    var arrayTwo: [UIImage]
}