[英]UIScrollView disable top bounce
I have a ViewController that contains an UIScrollView and I'm trying to disable the bounce effect only on the top of the view.我有一个包含 UIScrollView 的 ViewController,我试图仅在视图顶部禁用反弹效果。
I tried to add this answer in my ViewController class, which is saying to do this :我试图在我的 ViewController 类中添加这个答案,也就是说要这样做:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//disable bounce only at the top of the screen
scrollView.bounces = scrollView.contentOffset.y > 100
}
But I don't really understand what to do with this.但我真的不明白该怎么做。 I also tried to implement the UIScrollViewDelegate.我还尝试实现 UIScrollViewDelegate。
Nothing I tried worked.我试过的都没有用。
Here is my StoryBoard and ViewController这是我的 StoryBoard 和 ViewController
What do I need to do in order to make it works ?我需要做什么才能使其正常工作?
My setup :我的设置:
连接 scrollView 插座,然后尝试在viewDidLoad()
函数中添加scrollView.delegate = self
或添加scrollView.bounces = scrollView.contentOffset.y > 100
。
If you want to disable bounces of the scrollview
then you can take outlet of your scrollview
and can set bounces property to false in viewdidload
like below,如果您想禁用scrollview
的反弹,那么您可以使用滚动视图的出口,并可以在scrollview
viewdidload
反弹属性设置为 false,如下所示,
yourScrollView.bounces = false
or you can uncheck bounces from the storyboard also!或者您也可以取消选中情节提要中的反弹!
try this one试试这个
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView.contentOffset.y < 0 {
scrollView.contentOffset.y = 0
}
}
Okay here is the answer to my problem :好的,这是我的问题的答案:
viewDidLoad()
function, add this myScrollView.delegate = self
在viewDidLoad()
函数中,添加这个myScrollView.delegate = self
class MyViewController: UIViewController, UIScrollViewDelegate { ...
让你的 viewController 实现 UIScrollViewDelegate : class MyViewController: UIViewController, UIScrollViewDelegate { ...
Then add the following function :然后添加以下函数:
func scrollViewDidScroll(scrollView: UIScrollView) { myScrollView.bounces = myScrollView.contentOffset.y > 100 } func scrollViewDidScroll(scrollView: UIScrollView) { myScrollView.bounces = myScrollView.contentOffset.y > 100 }
Go to Attributes Inspector.转到属性检查器。 uncheck "Bounce On Scrolll"取消选中“滚动反弹”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.