【发布时间】:2013-07-16 08:22:38
【问题描述】:
遇到与this question 类似的问题,我正在尝试将双击手势识别器添加到我的UICollectionView 实例中。
我需要防止默认单击调用UICollectionViewDelegate方法collectionView:didSelectItemAtIndexPath:。
为了实现这一点,我实现了代码straight from Apple's Collection View Programming Guide (Listing 4-2):
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; NSArray* recognizers = [self.collectionView gestureRecognizers]; // Make the default gesture recognizer wait until the custom one fails. for (UIGestureRecognizer* aRecognizer in recognizers) { if ([aRecognizer isKindOfClass:[UITapGestureRecognizer class]]) [aRecognizer requireGestureRecognizerToFail:tapGesture]; } // Now add the gesture recognizer to the collection view. tapGesture.numberOfTapsRequired = 2; [self.collectionView addGestureRecognizer:tapGesture];
此代码未按预期工作:tapGesture 在双击时触发,但未阻止默认单击,并且仍调用委托的 didSelect... 方法。
在调试器中单步执行会发现 if 条件 [aRecognizer isKindOfClass:[UITapGestureRecognizer class]] 永远不会评估为真,因此新的 tapGesture 上的故障要求未建立。
每次通过for循环运行这个调试器命令:
po (void)NSLog(@"%@",(NSString *)NSStringFromClass([aRecognizer class]))
显示默认手势识别器(确实)不是UITapGestureRecognizer 实例。
相反,它们是私有类 UIScrollViewDelayedTouchesBeganGestureRecognizer 和 UIScrollViewPanGestureRecognizer。
首先,我不能在不违反私有 API 规则的情况下明确使用这些。其次,通过requireGestureRecognizerToFail: 附加到UIScrollViewDelayedTouchesBeganGestureRecognizer 似乎无论如何都不能提供所需的行为——即仍然调用代理的didSelect...。
如何使用UICollectionView 的默认手势识别器向集合视图添加双击并防止默认单击也触发委托的collectionView:didSelectItemAtIndexPath: 方法?
提前致谢!
【问题讨论】:
-
实现
collectionView:didSelectItemAtIndexPath:还不够吗? -
@cahn:不用担心——除了标准行为之外,我还需要双击。
-
This question 似乎解决了您遇到的同样问题。有用吗?
-
@AshFurrow:恐怕不行——我的双击工作正常,但我(也)不能停止 didSelect... 也被调用了。关于 shouldSelect... 和 shouldDeselect... 的建议看起来很有趣,但“呃...,这不对”的评论让我有点不确定如何继续。
标签: uigesturerecognizer uicollectionview uitapgesturerecognizer