使用 Swift 中的两个集合视图同步滚动? [英] Synchronize scrolling with two collection views in Swift?

查看:27
本文介绍了使用 Swift 中的两个集合视图同步滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种做法是滚动带有单元格的集合视图,然后根据其内容偏移量滚动另一个集合视图?

Is there a practice to scroll a collection view with cells, and, depending on its content offset, scroll the other collection view?

我有一个主集合视图,它有两个单元格并水平滚动.然后我有第二个集合视图,其中包含垂直滚动的单元格.发生的情况是,当我向上滚动,然后水平滚动到第二个垂直 cv 时,它与第一个不同步.

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.

我覆盖了垂直滚动集合视图中的 scrollViewDidScroll(不是主要的,因为它是水平滚动的),但无济于事(** 表示无法达到我的效果的部分).

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).

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**
    }

}

这是一个视觉效果:

注意当我滚动到第二个单元格时上方有空间吗?我希望垂直滚动视图跟随第一个.有什么建议吗?

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?

我在第一个单元格中滚动时尝试访问水平集合视图的第二个单元格,当我在第一个单元格中向下滚动时尝试更改 contentOffSet.y,但出现 EXC_BAD_ACCESS 错误.

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.

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

推荐答案

当滚动到 collectionViewB 并且 animatedfalse 所以它看起来是同步的.

You can do something like this when scrolled to the collectionViewB with animated to false so it look like it is synchronised.

let indexPath = collectionViewA.indexPathsForVisibleItems().first
self.collectionViewB.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Top, animated: false) 

不确定您是如何设置这两个集合的.如果您无权访问 collectionViewA,那么您可能需要将当前的 indexPath 保存到一个变量中并将其传递.

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.

这篇关于使用 Swift 中的两个集合视图同步滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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