简体   繁体   中英

Capturing combined button press and pan gesture

I need to capture the following touch events on iOS and interpret them:

  1. Finger A touch down within a UIButton (stays down)
  2. Finger B performs a pan gesture elsewhere on the screen, providing continuous callbacks. (I plan to use a UIPanGestureRecognizer to implement this functionality).
  3. Finger A touch up within the same UIButton

In essence, pressing and holding the UIButton puts the app into a special mode which lasts only as long as the button is held. Pan gestures performed during this time do different things to when the button is not pressed.

What I've tried so far:

  • hooking up the UIButton Touch Down and Touch Up Inside to IBActions in my UIViewController subclass. I've also
    • Problem encountered: the Touch Up Inside action is not called when another gesture happens on the screen from another finger while the button is pressed. This is because the original touch is cancelled.
  • attaching a UITapGestureRecognizer to the UIButton
    • Problem encountered: This only fires when the finger leaves the screen, hence it cannot be used to put the app into a special mode while the button is pressed.

I need to use a UIButton rather than a UIView so that the button has correct highlighting behaviour when pressed.

What is the best overall approach, given the problems I've encountered so far?

Use UILongPressGestureRecognizer with short minimumPressDuration on the "button" http://developer.apple.com/library/ios/#documentation/uikit/reference/UILongPressGestureRecognizer_Class/Reference/Reference.html#//apple_ref/occ/cl/UILongPressGestureRecognizer

Important thing is to use gesture recognizer delegate to make sure that gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: returns YES for these recognizers. Then, when your UIPanGestureRecognizer calls the event handler, you can check the state of your UILongPressGestureRecognizer and if the button isn't pressed just ignore the pan gesture.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM