开发者

Control UIScrollView outside bounds?

开发者 https://www.devze.com 2023-03-26 11:21 出处:网络
I\'m wondering how i can allow the user to scroll outside the bou开发者_JAVA技巧nds of a UIScrollView?You can try forwarding touch events from the various UIView methods of the superview to the scroll

I'm wondering how i can allow the user to scroll outside the bou开发者_JAVA技巧nds of a UIScrollView?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消