我已经在UICollectionView中为UIScrollView设置了UITapGestureRecognizer。我已将其配置为正确检测敲击并触发我编写的方法,但是如果我尝试将选择器设置为collectionView:didSelectItemAtIndexPath:轻按单元格时程序崩溃。
知道为什么会这样吗?
这有效:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
- (void) tapped:(UIGestureRecognizer *)gesture{
//some code
}
这不起作用:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collectionView:didSelectItemAtIndexPath:)];
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//some code
}
最佳答案
您编写的代码,
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collectionView:didSelectItemAtIndexPath:)];
选择器通常只是具有一个输入自变量(
UITapGestureRecogniser
对象)的singleFunction。应该是这样的
-(void)clicked:(UIGestureRecogniser *)ges{
}
但是您使用的选择器使用不当,因为它需要两个不能随手势识别器一起提供的输入,因此崩溃。
将上面的代码更改为下面的代码,
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clicked:)];
-(void)clicked:(UIgestureRecogniser *)ges{
//use gesture to get get the indexPath, using CGPoint (locationInView).
NSIndexPath *indexPath = ...;
[self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
}
关于ios - iOS为什么不能从UITapGestureRecognizer调用方法collectionView:didSelectItemAtIndexPath :?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21300732/