【问题标题】:use struct to display image for certain length of time使用 struct 显示图像一段时间
【发布时间】:2020-12-17 22:59:58
【问题描述】:

我希望下面的 swift 代码获取结构数据并用于类似 for 循环的内容。循环应该采用 arrayOne 并在图像应该在 imageview pic 上显示多长时间的时间内使用它。 Uiimage 数组应该对应于 int。所以有 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]
}

【问题讨论】:

  • 这和你的previous问题有什么区别?
  • 这里是一个相关的问题,但仅适用于音频。但是在这里您可以重用它来显示图像。请在此处检查此答案stackoverflow.com/questions/63494572/…
  • 就像我上面的回答一样,您需要运行计时器并在图像结束时隐藏图像,然后检查这是否是数组中的最后一个 int,如果不是 - 运行新计时器。你需要一些明确的例子吗?或者你会尝试像我的例子中那样使用上面的计时器?

标签: swift for-loop struct integer uiimage


【解决方案1】:

完整的示例。检查你是否一切都好!

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]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 2012-12-20
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    相关资源
    最近更新 更多