当用户单击按钮时,如何在几秒钟内更改图像,然后在swift中恢复到原始图像?
您好,我是新的编码与Swift,我也是相当新的Stackoverflow。我如何制作一个按钮,改变图像大约5秒钟,然后在用户点击按钮时返回到原始图像 我试过使用这个代码当用户单击按钮时,如何在几秒钟内更改图像,然后在swift中恢复到原始图像?,swift,Swift,您好,我是新的编码与Swift,我也是相当新的Stackoverflow。我如何制作一个按钮,改变图像大约5秒钟,然后在用户点击按钮时返回到原始图像 我试过使用这个代码 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), { gauntletImage.image = UIImage(named: gaun
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), {
gauntletImage.image = UIImage(named: gauntlet["gauntlet2"]) //change back to the old image after 5 sec
});
但我一直有以下两个错误:
无法将“Int”类型的值转换为预期的参数类型“dispatch\u time\t”(也称为“UInt64”)
及
“dispatch\u get\u main\u queue()的用法不明确”
这是我正在使用的更多代码
@IBOutlet weak var gauntletImage: UIImageView!
let gauntlet = ["gauntlet1", "gauntlet2", "gauntlet3", "gauntlet4", "gauntlet5", "gauntlet6",]
@IBAction func stonePressed(_ sender: UIButton) {
print(sender.tag)
gauntletImage.image = UIImage(named: gauntlet[sender.tag - 1]) //change to the new image
dispatch_after(dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), (Int64)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), {
gauntletImage.image = UIImage(named: gauntlet["gauntlet2"]) //change back to the old image after 5 sec
});
}
你可以试试
@IBAction func stonePressed(_ sender: UIButton) {
// store old image assuming it has an initial image in storyboard
let oldImg = gauntletImage.image!
// set new image
gauntletImage.image = UIImage(named: gauntlet[sender.tag - 1])
// wait 5 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 5 ) {
// set back old image
self.gauntletImage.image = oldImg
}
}
用户可以多次单击它,这样您就可以
sender.isEnabled = false
在dispatch after块中存储旧图像并将其设置回true
时,您可以尝试
@IBAction func stonePressed(_ sender: UIButton) {
// store old image assuming it has an initial image in storyboard
let oldImg = gauntletImage.image!
// set new image
gauntletImage.image = UIImage(named: gauntlet[sender.tag - 1])
// wait 5 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 5 ) {
// set back old image
self.gauntletImage.image = oldImg
}
}
用户可以多次单击它,这样您就可以
sender.isEnabled = false
存储旧图像并将其设置回调度后块内的
true
时您使用哪种swift版本?@Sh_Khan我使用的是swift 5您使用的是哪种swift版本?@Sh_Khan我使用的是swift 5