Forward touches to a UIScrollView

后端 未结 4 1285
一个人的身影
一个人的身影 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:03

    As an addition to solution of @johnboiles. When you catch the entire rect, then touches on elements inside the scrollview will be skipped. You could add aditional touch areas and for the normal scrollview just pass it on to the super.hitTest like this:

    class MagicScrollView: UIScrollView {
        override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
            if CGRectContainsPoint(CGRect(x: self.bounds.origin.x - 30, y: self.bounds.origin.y, width: 30, height: self.bounds.size.height), point) {
                return self
            }
            if CGRectContainsPoint(CGRect(x: self.bounds.origin.x + self.bounds.size.width, y: self.bounds.origin.y, width: 30, height: self.bounds.size.height), point) {
                return self
            }
            return super.hitTest(point, withEvent: event)
        }
    }
    

提交回复
热议问题