gpt4 book ai didi

swift - UILongPressGesture 被调用两次

转载 作者:行者123 更新时间:2023-11-30 10:04:33 28 4
gpt4 key购买 nike

因此,当我长按一个按钮时,它会识别长按,但“test”会被调用两次。我该如何防止这种情况发生?

@IBOutlet weak var button2: UIButton!

func longPressMe(){
print("test")
}

func longPressGes(){
let longpress = UILongPressGestureRecognizer(target: self, action: "longPressMe")
longpress.minimumPressDuration = 1
button2.addGestureRecognizer(longpress)
}


override func viewDidLoad() {
super.viewDidLoad()
longPressGes()
}

最佳答案

您必须检查手势识别器的状态。将 longPressMe() 更改为如下所示:

func longPressMe(recognizer: UILongPressGestureRecognizer) {
guard recognizer.state == .Began else { return }

// do stuff here
}

关于swift - UILongPressGesture 被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36809401/

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