4

I'm wondering how i can allow the user to scroll outside the bounds of a UIScrollView?

Andrew
  • 15,935
  • 28
  • 121
  • 203
  • I don't understand what you mean. – Daniel Dickison Aug 04 '11 at 15:58
  • The user can scroll my uiscrollview as they would usually, but i want to make it so when they drag outside of the bounds of the uiscrollview, it still scrolls the uiscrollview. – Andrew Aug 04 '11 at 16:04
  • Doesn't UIScrollView do that by default? – Daniel Dickison Aug 04 '11 at 16:06
  • When they start the drag outside the uiscrollview, i want the uiscrollview to scroll. – Andrew Aug 04 '11 at 16:09
  • Ohhh I see. That's definitely trickier. You can try forwarding touch events from the various UIView methods of the superview to the scrollview, and see if that will work. Or you might try using a `UIPanGestureRecognizer` on the superview and explicitly set the scroll view offset when you get the pan events. – Daniel Dickison Aug 04 '11 at 16:17
  • @Daniel Dickison: Your comment is a good answer. You can write it as an answer and i would like to upvote it for the solution you have given. – Praveen S Aug 04 '11 at 16:21
  • done, and added some rough examples. – Daniel Dickison Aug 04 '11 at 16:28
  • Take a look at [UIScrollView horizontal paging like Mobile Safari tabs][1], this is where i found a solution to your question. [1]: http://stackoverflow.com/questions/1220354/uiscrollview-horizontal-paging-like-mobile-safari-tabs – AlBeebe Sep 21 '11 at 15:08

2 Answers2

6

Try subclassing the UIScrollView and overriding hitTest:withEvent: so that the UIScrollView picks up touches outside its bounds. Something like this:

@interface MagicScrollView : UIScrollView
@end

@implementation MagicScrollView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    // Intercept touches 100pt outside this view's bounds on all sides
    if (CGRectContainsPoint(CGRectInset(self.bounds, -100, -100), point)) {
        return self;
    }
    return nil;
}

@end

You may also need to override pointInside:withEvent: on the UIScrollView's superview, depending on your layout.

See the following question for more info: interaction beyond bounds of uiview

Community
  • 1
  • 1
johnboiles
  • 3,494
  • 1
  • 32
  • 26
6

You can try forwarding touch events from the various UIView methods of the superview to the scrollview, and see if that will work. E.g:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [scrollView touchesBegan:touches withEvent:event];
}
// etc

Or you might try using a UIPanGestureRecognizer on the superview and explicitly set the scroll view offset when you get the pan events. E.g:

- (void)handlePan:(UIPanGestureRecognizer *)pan
{
    scrollView.contentOffset = [pan translationInView:scrollView];
}
// Or something like that.
Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89
  • It can be tough to get the scrolling animation right when you "throw" the scrollview. That is, when `[pan velocityInView:pan.view]` is nonzero. – johnboiles Dec 08 '15 at 05:57