gpt4 book ai didi

ios - 点击时 UIImageView 图像更改延迟

转载 作者:行者123 更新时间:2023-11-30 12:16:47 25 4
gpt4 key购买 nike

我有一个带有点击手势识别器的 UIImageView。点击时我希望 UIImageView 在 2 个图像之间交替。我的代码实现了这一点,但是当 UIImageView 被快速重复点击时,UIImageView 似乎滞后并落后。

我的代码:

@IBOutlet weak var moneyButton: UIImageView!

let benchUpImage = UIImage(named: "benchUp")
let benchDownImage = UIImage(named: "benchDown")
var benchIsDown = false

func moneyButtonPressed(sender: UITapGestureRecognizer){

if benchIsDown == false{
moneyButton.image = benchDownImage
benchIsDown = true
}else{
moneyButton.image = benchUpImage
benchIsDown = false
}

}

最佳答案

swift 3.0

您可以简单地允许用户点击一段时间。考虑用下面的代码替换您的代码

var canUserTap = true
let delayTime = 0.4 //setDelayTimming in seconds
@IBAction func moneyButtonPressed( _ sender : UITapGestureRecognizer){
if(canUserTap){
if benchIsDown == false{
moneyButton.image = benchDownImage
benchIsDown = true
}else{
moneyButton.image = benchUpImage
benchIsDown = false
}
canUserTap = false
Timer.scheduledTimer(timeInterval: delayTime,
target:self,
selector:#selector(allowUserTap(_:)),
userInfo:nil,
repeats:false)
}

}

func allowUserTap(_ timer:Timer){
canUserTap = true

}

关于ios - 点击时 UIImageView 图像更改延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45339043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com