gpt4 book ai didi

ios - 当我第一次快速点击按钮时,按钮单击不适用于 UITapGestureRecognizer

转载 作者:行者123 更新时间:2023-11-30 12:10:53 29 4
gpt4 key购买 nike

我已经在按钮单击时实现了 UITapGestureRecognizer 和 UILongPressGestureRecognizer。我的问题是,当我第一次点击按钮时,它不起作用,但第二次按下按钮时,它开始工作。

let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
longGesture.minimumPressDuration = 0.5
button.addGestureRecognizer(longGesture)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:)))
tapGesture.delegate = self
tapGesture.numberOfTouchesRequired = 1
tapGesture.numberOfTapsRequired = 1
button.addGestureRecognizer(tapGesture)
@objc func longTap(_ sender: UIGestureRecognizer){
print("long tap")

}
@objc func normalTap(_ sender: UIGestureRecognizer){

print("Normal tap")
}

最佳答案

试试这个

class ViewController: UIViewController, UIGestureRecognizerDelegate {

@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
longGesture.minimumPressDuration = 0.5
button.addGestureRecognizer(longGesture)
}


@IBAction func buttonClicked(_ sender: Any) {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:)))
tapGesture.delegate = self
tapGesture.numberOfTouchesRequired = 1
tapGesture.numberOfTapsRequired = 1
button.addGestureRecognizer(tapGesture)
}

@objc func longTap(_ sender: UIGestureRecognizer){
print("long tap")
}

@objc func normalTap(_ sender: UIGestureRecognizer){
print("Normal tap")
}
}

关于ios - 当我第一次快速点击按钮时,按钮单击不适用于 UITapGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46075318/

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