Forward touches to a UIScrollView

后端 未结 4 1283
一个人的身影
一个人的身影 2021-01-04 08:32

I have two view controllers. View controller A has a UIScrollView and presents view controller B. The presentation is interactive and controlled by the sc

4条回答
  •  再見小時候
    2021-01-04 09:19

    Try subclassing the UIScrollView and overriding hitTest:withEvent: so that the UIScrollView picks up touches outside its bounds. Then you get all the nice UIScrollView animations for free. 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. Customize this for the touch area you want to intercept
        if (CGRectContainsPoint(CGRectInset(self.bounds, -100, -100), point)) {
            return self;
        }
        return nil;
    }
    
    @end
    

    Or in Swift (Thanks Edwin Vermeer!)

    class MagicScrollView: UIScrollView {
        override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
            if CGRectContainsPoint(CGRectInset(self.bounds, -100, -100), point) {
                return self
            }
            return nil
        } 
    }
    

    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

提交回复
热议问题