[英]Synchronize scrolling with two collection views in Swift?
Is there a practice to scroll a collection view with cells, and, depending on its content offset, scroll the other collection view? 有没有一种做法可以滚动带有单元格的集合视图,并根据其内容偏移量来滚动另一个集合视图?
I have a main collection view that has two cells and scrolls horizontally. 我有一个具有两个单元格并水平滚动的主集合视图。 Then I have a second collection view with cells that scrolls vertically.
然后,我有了第二个集合视图,其中的单元格可以垂直滚动。 What happens is when I scroll up, then scroll horizontally to the second vertical cv, it's not synced with relation to the first.
发生的情况是,当我向上滚动然后水平滚动到第二个垂直简历时,它与第一个垂直简历没有同步。
I overrode the scrollViewDidScroll inside the vertically scrolling collection view (not the main one, because it scrolls horizontally), to no avail (** means the parts that don't work to achieve my effect). 我在垂直滚动的集合视图(不是主要视图,因为它是水平滚动)中覆盖了scrollViewDidScroll,但是没有用(**表示不能发挥作用的部分)。
var didScrollPastProfileView: Bool = false
func scrollViewDidScroll(scrollView: UIScrollView) {
let collectionViewDictionary = ["scrollView": self.collectionView]
NSNotificationCenter.defaultCenter().postNotificationName("cvDidScroll", object: nil, userInfo: collectionViewDictionary)
if scrollView.contentOffset.y > 250 {
**self.didScrollPastProfileView = true
if self.didScrollPastProfileView {
let previousContentOffset = scrollView.contentOffset.y
scrollView.contentOffset.y = previousContentOffset
}**
} else if scrollView.contentOffset.y < 250 {
**let previousContentOffset = scrollView.contentOffset.y
scrollView.contentOffset.y = previousContentOffset
self.didScrollPastProfileView = false**
}
}
Here's a visual: 这是一个视觉效果:
Notice how there's space above when I scroll to the second cell? 请注意,当我滚动到第二个单元格时上方有空间吗? I want that vertical scroll view to follow the first.
我希望该垂直滚动视图遵循第一个。 Any suggestions?
有什么建议么?
EDIT: I tried to access the second cell of the horizontal collection view while I was scrolling in the first cell, and I tried to change contentOffSet.y when I scrolled down in the first one, but I got an EXC_BAD_ACCESS error. 编辑:在第一个单元格中滚动时,我尝试访问水平集合视图的第二个单元格;在第一个单元格中向下滚动时,我尝试更改contentOffSet.y,但出现EXC_BAD_ACCESS错误。
if scrollView.contentOffset.y <= 250.0 {
let groupsPosts = childViewControllerForPosts.collectionView(childViewControllerForPosts.collectionView!, cellForItemAtIndexPath: NSIndexPath(forItem: 1, inSection: 0)) as! FeedCell
groupsPosts.collectionView.contentOffset.y = -scrollView.contentOffset.y
self.topVerticalConstraint?.constant = -scrollView.contentOffset.y
You can do something like this when scrolled to the collectionViewB
with animated
to false
so it look like it is synchronised. 当滚动到
collectionViewB
时,如果将其animated
为false
,则可以执行类似的操作,以使其看起来已同步。
let indexPath = collectionViewA.indexPathsForVisibleItems().first
self.collectionViewB.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Top, animated: false)
Not exactly sure how you set up this two collection. 不完全确定如何设置这两个集合。 If you do not have access to
collectionViewA
then you might have to save the current indexPath to a variable and pass it over. 如果您无权访问
collectionViewA
则可能必须将当前的indexPath保存到变量并将其传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.