【发布时间】:2019-12-21 23:47:18
【问题描述】:
首先,已经回答的问题都没有帮助我
- Swift: Long Press Gesture Recognizer - Detect taps and Long Press
- Using tap gesture and long press at the same time in Table View
- Long Press Gesture Recognizer Only Fired When Finger is Lifted
- 等等
除了一件事之外,代码几乎可以正常工作:长按手势仅在我将手指从屏幕上抬起时才会调用。但我需要像在 Instagram 快拍中那样的行为(当您可以在快拍之间切换并按住手指暂停某个快拍时)。
我的问题更多是关于如何在用户触摸手指向下而不是向上触摸时强制 UILongPressGesture 触发。
这是我的代码:
private func setupTapGestures() {
tapRecognizer = UITapGestureRecognizer()
tapRecognizer?.addTarget(self, action: #selector(handleTapGesture(_:)))
tapRecognizer?.delegate = self
view.addGestureRecognizer(tapRecognizer!)
longPressRecognizer = UILongPressGestureRecognizer()
longPressRecognizer?.addTarget(self, action: #selector(handleLongPressGesture(_:)))
longPressRecognizer?.minimumPressDuration = 0.1
longPressRecognizer?.delegate = self
view.addGestureRecognizer(longPressRecognizer!)
}
@objc func handleTapGesture(_ gestureRecognizer: UIGestureRecognizer) {
let width = view.frame.width
let point = gestureRecognizer.location(in: view)
viewModel?.tapAction(viewWidth: width, tapPoint: point)
Swift.print("Tap gesture")
}
@objc func handleLongPressGesture(_ gestureRecognizer: UILongPressGestureRecognizer) {
if gestureRecognizer.state == .began {
Swift.print("Began")
} else if gestureRecognizer.state == .ended {
Swift.print("Ended")
}
}
UIGestureRecognizerDelegate:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// Don't recognize a single tap until a long-press fails
if gestureRecognizer == tapRecognizer && otherGestureRecognizer == longPressRecognizer {
return true
}
return false
}
有什么建议或想法吗?
【问题讨论】:
标签: ios swift uigesturerecognizer uitapgesturerecognizer uilongpressgesturerecogni