手指离开视图时停止UILongPressGestureRecognizer

时间:2019-03-26 20:24:46

标签: ios cocoa-touch uiview uigesturerecognizer

当触发UILongPressGestureRecognizer的触摸离开手势时,我该如何停止甚至检测呢?在UIButton上可以看到这种行为,当手指离开按钮时,触摸事件将停止。

1 个答案:

答案 0 :(得分:1)

您可以使用以下简单代码段检测出入:

 @objc   func handleLongPress(_ recognizer:UILongPressGestureRecognizer) {


switch recognizer.state {
 case .changed:
    if let view = recognizer.view {
        if view.bounds.contains(recognizer.location(in: recognizer.view) ) {
              print("inside view")
        }
        else {
            print("out of view")
        }
    }
  default:
    break;
  }
  }