問題描述
我有一個 UIScrollView 正在滾動一個相當大的 UIView.
I have a UIScrollView that is scrolling a fairly large UIView.
在某些時候我想限制用戶可以滾動的區域.例如,我可能只想讓他們查看視圖的底部四分之一.
At certain times I want to limit the area the user can scroll around in. For example, I may only want to allow them to view the bottom quarter of the view.
如果視圖滾動得太遠,我可以通過覆蓋 scrollViewDidScroll 然后調用 setContentOffset 來限制區域.但是這樣我就不能讓它像 UIScrollView 在滾動超出 UIView 的邊界時那樣平滑地反彈.
I am able to limit the area by overriding scrollViewDidScroll and then calling setContentOffset if the view has scrolled too far. But this way I can't get it bounce back as smoothly as the UIScrollView can naturally do when scrolling beyond the bounds of the UIView.
有沒有更好的方法來限制 UIScrollView 中的可滾動區域?
Is there a better way to limit the scrollable area in a UIScrollView?
推薦答案
我會將滾動視圖的 contentSize 屬性更改為您希望用戶能夠滾動的區域的大小并調整 frame.origin您想要的子視圖的左上邊界相對于滾動視圖顯示在 (0, 0) 處.例如,如果您的視圖高度為 800 磅,并且您想顯示底部四分之一,請將 contentSize 的高度設置為 200,并將 view.frame.origin 的 y 分量設置為 -600.
I would change the contentSize property of the scroll view to the size of the area you want the user to be able to scroll around in and adjust the frame.origin of the subview such the upper left boundary you want appears at (0, 0) relative to the scroll view. For example, if your view is 800 points tall and you want to show the bottom quarter, set the height of contentSize to 200 and set the y component of view.frame.origin to -600.
這篇關于限制 UIScrollView 中的可滾動區域的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!