Ios 如何根据Swift 3中的时间间隔创建if else语句

Ios 如何根据Swift 3中的时间间隔创建if else语句,ios,swift,Ios,Swift,现在我的应用程序有一个以秒为单位的计时器。我希望代码能像这样工作。计时器达到10秒时,显示图像A,否则显示图像B 设置图像a。使用定时计时器在10秒内设置imageB: imageView.image = imageA Timer.scheduledTimer(withTimeInterval: 10, repeats: false) { _ in imageView.image = imageB } 注意:带块使用计时器仅在iOS 10及更高版本上可用。如果您支持较旧的iOS版本,则

现在我的应用程序有一个以秒为单位的计时器。我希望代码能像这样工作。计时器达到10秒时,显示图像A,否则显示图像B

设置图像a。使用定时计时器在10秒内设置imageB:

imageView.image = imageA
Timer.scheduledTimer(withTimeInterval: 10, repeats: false) { _ in
    imageView.image = imageB
}
注意:带块使用计时器仅在iOS 10及更高版本上可用。如果您支持较旧的iOS版本,则需要使用带有选择器的版本

如果每秒调用一次updateScoreTime例程并执行更多仅管理图像的操作,则可以在属性中保留秒数计数:

imageView.image = imageA
scoreTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateScoreTime), userInfo: nil, repeats: true)
count = 0
当计时器弹出时:

func updateScoreTime(_ timer: Timer) {
    // update score time
    ...

    // change picture if >= 10 seconds
    count += 1
    if count >= 10 {
        imageView.image = imageB
    }
}

所以只需将图像设置为您正在谈论的图像,我不知道,因为您没有向图像a发布任何有意义的代码,然后启动一个计时器,在10秒后将图像设置为b。为什么每次都要重新安排计时器?使用一个重复计时器,并减少或增加一个计数器变量。使用计数器的值确定要显示的图像
func updateScoreTime(_ timer: Timer) {
    // update score time
    ...

    // change picture if >= 10 seconds
    count += 1
    if count >= 10 {
        imageView.image = imageB
    }
}