patman
patman

Reputation: 2840

iOS gestureRecognizer not handled subviews

i have the following problem: in my iphone app i have a UIView that holds 0 to several subviews. Those subviews may overflow the parent and therefore be hidden and all of them are UIButtons. I added a UISwipeGestureRecognizer to the UIView to move the buttons around which works great. However it only works when the gesture is done on background, the UIButtons interfere with the gesture recognizer.

How i can i pass the gesture through? Btw. i still need the tap of the Buttons.

Thanks!

EDIT:

I tried to add the gesture recognizer to the UIButtons too, but it is not triggered... Although performing the swipe gesture prevents the UIButton from going to highlighted state it doesn't trigger the gesture. I added setDelaysTouchesBegan:YES as suggested in UIButton and Swipe Gesture. That's how i do it right now:

UIButton *breadcrumb = [UIButton buttonWithType:UIButtonTypeCustom];
[breadcrumb setImage:[UIImage imageNamed:@"Next"] forState:UIControlStateNormal];
[breadcrumb setTitle:title forState:UIControlStateNormal];
[breadcrumb.titleLabel setFont:[UIFont systemFontOfSize:12.0f]];
[breadcrumb sizeToFit];
[breadcrumb setTag:level];


UISwipeGestureRecognizer *gr = [[UISwipeGestureRecognizer alloc] init];
[gr setDelegate:self];
[gr setDirection:UISwipeGestureRecognizerDirectionRight];
[gr setDelaysTouchesBegan:YES];
[self.tableView addGestureRecognizer:gr];
[breadcrumb addGestureRecognizer:gr];

EDIT 2:

I have now subclassed UIButton and initilizing it now like so: [BreadcrumbButton buttonWithType:UIButtonTypeCustom]. In the initializer i added the button itsself as a listener to all touch events [self addTarget:self action:@selector(eventReceiver:) forControlEvents:UIControlEventAllTouchEvents]; to inspect whats going on.

- (void)eventReceiver:(UIButton *)btn {
    NSLog(@"Reveived event: %@ ---------------", btn);

    for(UIGestureRecognizer *gr in ev.gestureRecognizers) {
        NSLog(@"Gesture: %@", gr);
    }
}

What i see is that a) the button has just one gesture recognizer added and b) that this UISwipeGestureRecognizer jumps to state Possible during swipe but does not forward to its delegate methods.

Upvotes: 1

Views: 822

Answers (1)

TheCodingArt
TheCodingArt

Reputation: 3429

You would have to subclass the UIButton and over ride the tap delegate callback and forward this call to whatever is handling a UISwipeGestureRecognizer. Unless you add the gesture recognizer on the UIButton, it will always call it's touch handler before the view behind it. You can also explicitly tell the button to not handle it's touch events thus passing the touch event down the chain (via userInteractionEnabled), but as you've already stated you do not want this. The best way to go about this would be by creating a subclass of UIButton and handling the touch events there and/or forwarding the events using delegation. Pressing the button is a touch event, so you may just want to add a tap gesture recognizer to the button and call the IBAction from that and then have the swipegesturerecognizer forward a delegate call.

Upvotes: 2

Related Questions