获取uicollectionviewcell的触摸位置

Loo*_*zie 9 objective-c ios uicollectionview uicollectionviewcell

我试图在所选的UICollectionviewCell中获取触摸位置.我有一个UIViewController,vc,包含一个UICollectionView,collectionView作为它的视图.collectionView内部是UICollectionViewCells.vc符合UICollectionViewDelegate所以最好我想在委托回调中获取触摸位置collectionView:(UICollectionView *)collection didSelectItemAtIndexPath:(NSIndexPath *)indexPath,(如果可能的话).有关如何做到这一点的任何建议?谢谢.

+--------------------------------------------+
| UICollectionView                           |
|                                            |
|  +-------------+       +-------------+     |
|  |  UICollec   |       |             |     |
|  |  tionView   |       |             |     |
|  |  Cell       |       |     *<---------------<Touch here
|  |             |       |             |     |
|  +-------------+       +-------------+     |
|                                            |
+--------------------------------------------+    
Run Code Online (Sandbox Code Playgroud)

*更新* 我最终检测到UIViewCollectionView使用UIGestureRecognizer的点击,然后将点击点转换为UICollectionViewCell视图.如果有更好的方法,请告诉我.谢谢.

-(void) viewDidLoad {
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [tapGesture setNumberOfTapsRequired:1];
    [self.collectionView addGestureRecognizer:tapGesture];
}

-(void) handleTap:(UIGestureRecognizer*) gesture {
    if (gesture.state == UIGestureRecognizerStateEnded) {
    CGPoint tappedPoint = [gesture locationInView:_collectionView];
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:tappedPoint];
    CollectionViewCell *cell = (CollectionViewCell*)[self.collectionView cellForItemAtIndexPath:indexPath];
    CGPoint pointWRTCell = [cell convertPoint:tappedPoint fromView:self.collectionView];
    NSLog(@"collectionView point(%1.1f,%1.1f); cell point (%1.1f,%1.1f)",
          tappedPoint.x,tappedPoint.y,pointWRTCell.x,pointWRTCell.y);
    }
}
Run Code Online (Sandbox Code Playgroud)

Cri*_*ris 6

您可能需要一个自定义单元格来执行此操作.didSelectItemAtIndexPath只告诉你一个细胞被选中.不是它被触摸的细胞内部.