UICollectionView滑动

4

我希望能够在一个占据整个屏幕宽度的collectionView中检测到用户向左或向右滑动。不添加手势识别器是否也可以实现?我已经尝试添加手势识别器,但只有当我们将collectionView的scrollEnabled属性设置为NO时才有效。

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeRight:)];
swipeRight.delegate = self;
swipeRight.numberOfTouchesRequired = 1;
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeLeft:)];
swipeLeft.delegate = self;
swipeLeft.numberOfTouchesRequired = 1;
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];

[self.collectionView addGestureRecognizer:swipeLeft];
[self.collectionView addGestureRecognizer:swipeRight];

为什么你想要使用滑动手势? - ChintaN -Maddy- Ramani
我想在检测左滑还是右滑时进行一些处理。 - user694688
集合视图是垂直滚动还是水平滚动?实际问题是您想在集合视图上检测左/右滑动,还是您真的不希望手势识别器这样做? - Stephen Johnson
水平方向,我不想使用手势识别器,因为我将此集合视图作为另一个集合视图的单元格。 - user694688
UICollectionView是一种滚动视图,所以您可以重写scrollViewDidScroll方法来检查滚动的方向。请参考此答案-[https://dev59.com/mHE85IYBdhLWcg3w64IA#4073028] - Pratik Mistry
4个回答

2
也许您已经禁用了用户交互。您检查过了吗?并将手势定义为该类的属性。
self.collectionView.setUserInteractionEnabled=true;

1
尝试覆盖scrollViewWillEndDragging:scrollView withVelocity:velocity targetContentOffset:targetContentOffset,因为集合视图继承自滚动视图。这样,您应该能够评估速度参数以确定方向。因此,您需要实现UIScrollViewDelegate协议。

1

我给collectionView添加了滑动手势。

它可以正常工作。

就像Stephen Johnson所说的那样,我猜你的单元格有一个scrollView。

所以它会阻止手势。


0

我不确定我完全理解你的问题背景。我看到你在一个集合视图中有另一个集合视图。我假设外部集合视图垂直滚动,内部集合视图水平滚动。你需要在两个集合的手势识别器之间建立一个失败依赖关系。

//Data source for your outer collection view
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   ...
   UICollectionView* innerCollectionView = ...;

   [collectionView. panGestureRecognizer requireGestureRecognizerToFail:innerCollectionView. panGestureRecognizer];

   ...
}

如果嵌套的集合视图中还有更多内容,您能否提供更多细节?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接