Make UIScrollView work only on content part, not “sides”

泄露秘密 提交于 2019-12-11 04:17:41

问题


Here's a scroll view (it is the GRAY area). Content area is yellow. Notice it insets, so that you can scroll the sides of the content over to the middle of the screen.

PROBLEM: you can put your finger on the GRAY area and scroll it.

I want it to work so that you can ONLY scroll by putting your finger on the yellow content. If you put your finger on the gray "background" and try to scroll it, nothing should happen.

How to do this?


Yellow content is scrolled fully to the right, it rests here:

Yellow content is scrolled fully to the left, it rests here:


回答1:


Make a subclass of UIScrollView. Give it a reference to the yellow view. Override pointInside:withEvent: to return false unless the point is in the frame of the yellow view.

class MyScrollView: UIScrollView {

    @IBOutlet var yellowView: UIView?

    override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
        return yellowView?.frame.contains(point) ?? false
    }

}



回答2:


This should work. in scrollViewWillBeginDragging you remember the coordinates where dragging begins, in scrollViewDidScroll you determine if dragging began inside yellow area and just do nothing (allow scroll to happen normally), in other case you return ScrollView contentOffset property to the coordinates that it had at the moment of scrollViewWillBeginDragging



来源:https://stackoverflow.com/questions/37503604/make-uiscrollview-work-only-on-content-part-not-sides

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!