質問

I have an UIScrollView inside another UIScrollView. The outer scroll view handles paging, the inner scroll view handles zooming (it’s an image gallery with zooming support). I need the inner scroll view to ignore single touches so that they go to the outer scroll view instead.

This can be done using [innerScrollView setUserInteractionEnabled:NO], but that obviously also turns off the pinch and panning gestures on the inner scroll view. Is there a way to keep the pinch and pan gestures handled by the inner zoom view and forward all other events up the responder chain?

PS. I can always come with some delegation hack to solve the issue. I’d like to know if there’s a simple way to get it working using the regular responder chain.

役に立ちましたか?

解決

What I have found out: The single-tap event eventually makes its way to touchesBegan:withEvent: on the content view inside the inner scroll view. This content view does not implement the method and the default UIView implementation passes the event up the responder chain. Here we have the inner scroll view, which does implement touchesBegan:withEvent:. This implementation swallows the single tap and nothing goes up the responder chain. What helped is to subclass UIScrollView and send the touches up the responder chain manually:

- (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
{
    [[self nextResponder] touchesBegan:touches withEvent:event];
}

Of course it’s also good to forward at least touchesEnded:withEvent: this way. This does everything I need. The inner scroll view can handle zooming and panning, the outer scroll view receives the single-tap events. I can even zoom in and then pan so far to the right that I go to the next page.

他のヒント

On iOS 5, UIScrollViews expose their panGestureRecognizer and pinchGestureRecognizer.

So, if you don't need to support older iOS versions, you could try innerScrollView.panGestureRecognizer.minimumNumberOfTouches = 2.

EDIT: This does not work; the scrollView still swallows the touches. I'll leave this here for further reference.

If you don't want the inner scrollView to handle panning at all, you could probably (I have not tried it) just remove its panGestureRecognizer.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top