问题
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