gpt4 book ai didi

swift - 防止UIButton长按重复功能

转载 作者:行者123 更新时间:2023-11-28 13:29:58 24 4
gpt4 key购买 nike

以下 UILongPressGestureRecognizer 代码有效,但它会在按住按钮时重复运行 long() 按下功能。我想让它在用户按下时只运行一次。这可能吗?

override func viewDidLoad() {

let tapGesture = UITapGestureRecognizer(target: self, action: #selector (tap))
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
tapGesture.numberOfTapsRequired = 1
shiftBtn.addGestureRecognizer(tapGesture)
shiftBtn.addGestureRecognizer(longGesture)
}

@objc func tap() {
print("Short Tap")
}

@objc func long() {
print("Long press")
}

谢谢!

最佳答案

gesture 激活时,您应该调用 long。每当长 gesture 事件时,它就会被调用一次。

  @objc  func hitlongprass(_ sender : Any){

guard let longPress = sender as? UILongPressGestureRecognizer else
{ return }

if longPress.state == .began { // When gesture activated
long()
}
else if longPress.state == .changed { // gesture Calls multiple times by time changed

}
else if longPress.state == .ended { // When gesture end

}
}

func long() {
print("Long press")
}

调用:

let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(hitlongprass(_:)))

关于swift - 防止UIButton长按重复功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57541325/

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