简体   繁体   English

集合视图中的水平滚动

[英]Horizontal scroll in collection view

I wish to do something when my collection view cell is scrolled horizontally. 当我的收藏夹视图单元格水平滚动时,我想做些事情。 But I do not know which function gets called when it is getting scrolled. 但是我不知道滚动时哪个函数被调用。

I have a collection view cell inside of a table view cell, and the collection view cell scrolls horizontally 我在表格视图单元格内部有一个集合视图单元格,并且该集合视图单元格水平滚动

If you use storyboard, in Attributed Inspector you will find an option that Scroll Direction , make it Horizontal , default this is set to Vertical 如果使用情节提要,请在“ Attributed Inspector找到一个选项,将“ Scroll Direction设置为“ Horizontal ,默认将其设置为“ Vertical

programitically programitically

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
  1. First, set your collectionview's delegate to your viewcontroller (or view) 首先,将您的collectionview的委托设置为您的viewcontroller(或view)
  2. Implement UIScrollViewDelegate methods inside your viewcontroller and you can track your collectionview's scrolling. 在viewcontroller中实现UIScrollViewDelegate方法,您可以跟踪collectionview的滚动。

There are several methods that will notify you when a collection view gets scrolled, all of them are contained in UIScrollViewDelegate , and thus in UICollectionViewDelegate protocol, which inherits from the former. 有几种方法可以在滚动集合视图时通知您,所有方法都包含在UIScrollViewDelegate以及UICollectionViewDelegate协议中,该协议继承自前者。

  1. scrollViewDidScroll(_:) is indeed called each time the content offset changes . 实际上,每次内容偏移量更改,都会调用scrollViewDidScroll(_:) That means not only the active scrolling issued by the user, but also scrolling by inertia, programmatic scrolling and bouncing. 这不仅意味着用户进行了主动滚动,而且还意味着通过惯性滚动,程序化滚动和弹跳。 You can use this method to react to the scrolling distance, for example, by querying the contentOffset property of the scroll view. 您可以使用此方法来响应滚动距离,例如,通过查询滚动视图的contentOffset属性。
  2. scrollViewWillBeginDragging(_:) is, in contrast, getting called only at the beginning of scrolling issued by the user (that's why in the documentation you can see that this method may be called only after some delay, since the scroll view's gesture recognizer needs time to decide if it's a tap or a pan gesture). 相反, scrollViewWillBeginDragging(_:)仅在用户发出的滚动开始时被调用(这就是为什么在文档中您可以看到仅在经过一些延迟后才能调用此方法的原因,因为滚动视图的手势识别器需要时间确定是轻击还是平移手势)。 It will not be called again until the user lifts their finger and starts scrolling again. 在用户抬起手指并再次开始滚动之前,不会再次调用它。
  3. scrollViewWillBeginDecelerating(_:) is called when the user-issued scrolling discussed above ends, but the scroll view will continue scrolling further to achieve this inertia feeling. 当上述用户发布的滚动结束时,将调用scrollViewWillBeginDecelerating(_:) ,但是滚动视图将继续滚动以实现这种惯性感觉。 Again, it will not get called again until the user lifts their finger one more time. 再一次,直到用户再次抬起手指,它才会再次被调用。

That's basically it. 基本上就是这样。 If this still doesn't narrow down the event that you want to track (for example, you want to become notified when the user starts scrolling at the initial position only), you will need to set some flags or track additional properties. 如果这仍不能缩小您要跟踪的事件的范围(例如,您希望在用户仅在初始位置开始滚动时得到通知),则需要设置一些标志或跟踪其他属性。

To track horizontal scrolling for instance, you will need to either compare the scroll view's previous content offset to the current or check the scrolling velocity via scrollView.panGestureRecognizer.velocity(in: collectionView) . 例如,要跟踪水平滚动,您需要将滚动视图的上一个内容偏移量与当前内容进行比较,或者通过scrollView.panGestureRecognizer.velocity(in: collectionView)检查滚动速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM