如何检测 UICollectionView 中最后一个或第一个单元格的滑动

How to detect the swiping of the last or first cell in UICollectionView

我有一个包含一行的集合视图,如果用户试图在第一个单元格上向右滑动或试图在最后一个单元格上向左滑动,我想隐藏它。

仅添加 left\right 滑动手势是行不通的。 我设法通过向上滑动手势将其添加到第一个和最后一个单元格(在 cellForItemAtIndexPath 方法中)来完成此操作。

有什么想法吗?

获取滚动方向

@property (nonatomic) CGFloat lastContentOffset;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.lastContentOffset > scrollView.contentOffset.x)
    {
        NSLog(@"Scrolling left");
    }
    else if (self.lastContentOffset < scrollView.contentOffset.x)
    {
        NSLog(@"Scrolling right");
    }

    self.lastContentOffset = scrollView.contentOffset.x;
}

好的,我已经设法结合@Tejas 的回答和评论找到了解决方案:

var lastContentOffset = CGFloat()
var scrollDir = UISwipeGestureRecognizerDirection.Left

func scrollViewDidScroll(scrollView: UIScrollView)
{
    if (self.lastContentOffset > scrollView.contentOffset.x)
    {
        self.scrollDir = UISwipeGestureRecognizerDirection.Left
    }
    else if (self.lastContentOffset < scrollView.contentOffset.x)
    {
        self.scrollDir = UISwipeGestureRecognizerDirection.Right
    }
    self.lastContentOffset = scrollView.contentOffset.x;
}

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
    if let indexPath = self.upperCollectionView?.indexPathsForVisibleItems()[0]
    {
        if indexPath.item == 0 && self.scrollDir == UISwipeGestureRecognizerDirection.Left
        {
           //hide the collection view
        }
    }
}