Search code examples
iosevent-handlinguibuttontouch

Cancel touch on UIButton on touch move (iOS)


I have a view with button and touch-move gesture. When user touches button it becomes selected and keeps receiving touch-move events. I want to deselect the button on touch-move and pass moves to gesture.

How to cancel touch receiving on button "elegantly"?


Solution

  • You need to implement the UIGestureRecognizerDelegate. Specifically this method. Then just build your logic for deciding if the gesture should receive the touch or not. If you return NO then the touch is up for grabs for the button. if you return YES the gesture to take the touch.

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    

    If you never even more control I think you'll need to subclass the button and takeover its hit testing.