如何在将其组件之一移动到其边界附近时滚动 up/down UICollectionView?

How to scroll up/down a UICollectionView while moving one of its components near of its borders?

所以我有一个带有多个 row/column 的滚动条 UICollectionView

我可以在每个 UICollectionViewCell 中添加我称之为 PostIt 的东西(右边的 red/yellow/green 方块)。然后我还可以在每个 UICollectionViewCell.

之间移动每个 PostIt

我想做的是,当我(缓慢地)将一个 PostIt 拖到 UICollectionView 的 top/bottom 附近时,它将滚动 up/down UICollectionView.

我知道如何使用 UICollectionView 的滚动条位置:_collectionView.contentOffset.x(或使用 _collectionView.contentOffset.y,但我不太关心这个)。

我认为如果我能够将 PostIt 的中心坐标与集合视图的 top/bottom 的坐标与 if(the center of the postIt is near of the top/bottom) then {scroll up/down}.

之类的东西进行比较,那将是可能的

但我真的不知道如何处理,如果有人能帮助我,那就太好了!提前致谢!

这比你想象的要简单!

我用 UITableView 做过一次。 UICollectionView 应该表现相似。您必须实施 UIScrollViewDelegate 此代码应该可以帮助您:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.firstTableView)
    {
        [self.secondTableView setContentOffset:scrollView.contentOffset];
    }

    else if (scrollView == self.secondTableView)
    {
        [self.firstTableView setContentOffset:scrollView.contentOffset];
    }
}

我们可以从它的 属性 contentOffset 中得知 UITableView 滚动了多少(并改变了它)。 PostIt 的位置可以从另一个滚动视图(如果我们正在使用它)或拖动时的坐标来定义。当 PostIt 靠近所需位置时 - 更改 contentOffset 或将 UICollectionView 滚动到所需的索引路径。