iOS Swift:ScrollView 严格拖动 [英] iOS Swift : ScrollView strict dragging

查看:32
本文介绍了iOS Swift:ScrollView 严格拖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 scrollView(仅水平滚动),其中有两个集合视图,并且希望如果它被拖动/滑动到 >= 屏幕中间,那么它应该向前滚动,即右侧(显示第二个集合视图)否则它应该是在左侧(显示第一个集合视图).它不应该保持半滚动或完全滚动.我怎样才能做到这一点?

I have a scrollView(only horizontal scrolling) in which two collection views and want that if it is dragged/Swiped upto >= mid of screen then it should scroll forward i.e right side (show 2nd collection view) else it should be in left side(show first collection view). It should not stay half scrolled either full or none. How can i make this possible?

推荐答案

要实现滚动捕捉,要使用这个 UIScrollViewDelegate 方法:

To achieve a scroll snap, you want to use this UIScrollViewDelegate method:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)

您需要在此方法中设置以下内容.

You'll want to set the following within this method.

targetContentOffset.pointee.x = 

这里有一个 教程链接,可能有助于解释.

Here's a tutorial link that may help explain.

或者只是将此 ObjC SO answer 转换为 swift 并按照它的描述从垂直捕捉变为水平.

Or just convert this ObjC SO answer to swift and change from vertical snapping to horizontal as it describes.

这篇关于iOS Swift:ScrollView 严格拖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆