限制 UIScrollView 中的可滚动区域

Limiting the scrollable area in UIScrollView(限制 UIScrollView 中的可滚动区域)

本文介绍了限制 UIScrollView 中的可滚动区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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 中的可滚动区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:限制 UIScrollView 中的可滚动区域

基础教程推荐