Alex Rivera | Logout

UIButton that will ignore a 'drag' and pass it to UIScrollView

Asked 2011-05-13T10:10:18.800
10

I know there are similar posts on Stack but nothing that I've found has helped me find a solution.

  • I have a scrollview added to a UIView.
  • I have added a UIButton to the same UIView above the scrollview.

    1. I want the UIButton to respond to a touchUp and fire an event.
    2. If someone DRAGS on the UIButton I want it to pass the event to the scrollView and drag the UIScrollView without firing the button event.

I know how to pass on a touch with hitTest returning the scrollview but this stops anything getting to the UIButton.

I don't want to add the UIButton to the UIScrollView.

Does anyone have a proposed solution?

Edit
Report

1 Answer

1

Try adding a UIPanGestureRecognizer to the UIBUtton. In the callback you specify to handle the pan (drag), you need not implement anything. Then, set cancelsTouchesInView to YES. This will cause your button to passed drag gestures to the other views.

        UIPanGestureRecognizer *gr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan)];
        [gr setCancelsTouchesInView:YES];
        [myButton addGestureRecognizer:gr];
        [gr release];

And here's handlePan:

-(void)handlePan
{
    // Do nothing
}

I ran into a similar situation where I had a UIScrollView with two UIView subviews. Each UIView contained a UISlider. I wanted to catch (and drop) vertical drags (which is where the gesture recognizer came in), but I also needed horizontal scrolls to work in the slider as you'd expect AND to pan the scroll view left and right. This was the solution. (The gesture recognizer was attached to each UIView.)

answered 2011-07-28T00:55:04.110

Your Answer