ZbadhabitZ
ZbadhabitZ

Reputation: 2913

Subclassing UIGestureRecognizer and UILongPressGestureRecognizer

I am in a situation wherein I have a UIView that I have added a subclassed UIGestureRecognizer to. I am using the following code;

class ButtonGestureRecognizer: UIGestureRecognizer {

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
    //
    state = .began
  }

  override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
    //
    state = .ended
  }
}

This allows me to capture the touch "down" and touch "up" event, which I am using to scale the UIView larger when it is pressed down and back to its normal size when it is released.

I also need to add a UILongPressGestureRecognizer to this same view, wherein it will also scale up when held down, and return to its normal size when it is released, but perform a different action.

However, the subclassed Gesture Recognizer seems to be preventing the UILongPressGestureRecognizer from working. The only solution I've found so far is to give up my subclassed gesture recognizer and use a UITapGestureRecognizer (with minimum taps required set to one) and a UILongPressGestureRecognizer, but then I give up the ability to properly detect the tap/press began and end states.

Any way around this? Thanks!

Upvotes: 1

Views: 475

Answers (1)

EmilioPelaez
EmilioPelaez

Reputation: 19892

There's a protocol called UIGestureRecognizerDelegate and it has this method

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

If you conform to that protocol and set your object as the delegate of your recognizers, you can return true on that method and your gesture recognizers will work together.

Upvotes: 1

Related Questions