如何强制计数器从01开始而不是1(Swift)

时间:2018-06-06 07:46:07

标签: ios swift xcode counter

我有24个gif文件,当我想通过计数器编程Next按钮时,它从gif编号11开始,我希望从01开始,所以从字面上看它跳过前10个GIF!

var counter = 0
@IBOutlet weak var image: UIImageView!
@IBAction func next(_ sender: Any) {
    image.image = UIImage (named: "frame_\(counter)_delay-0.06s.gif")
    counter += 1
    if counter == 24 {
        counter = 0
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用String(format: "frame_%02d_delay-0.06s.gif",counter)

所以你将拥有以下代码,

let imageName = String(format: "frame_%02d_delay-0.06s.gif",counter)
image.image = UIImage (named: imageName)