我正在尝试获取所选UICollectionviewCell内的触摸位置。 我有一个UIViewController,vc,它包含一个UICollectionView,collectionView作为其视图。 在collectionView内部是UICollectionViewCells。 vc符合 UICollectionViewDelegate ,因此最好在委托回调中获取触摸位置,
collectionView:(UICollectionView *)collection didSelectItemAtIndexPath:(NSIndexPath *)indexPath ,(如果可能)。 有什么建议吗? 谢谢。
+--------------------------------------------+
| UICollectionView |
| |
| +-------------+ +-------------+ |
| | UICollec | | | |
| | tionView | | | |
| | Cell | | *<---------------<Touch here
| | | | | |
| +-------------+ +-------------+ |
| |
+--------------------------------------------+
*更新*
最终我使用了UIGestureRecognizer
检测了对UIViewCollectionView
的轻触,并将轻触点转换为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);
}
}
deleteButtonPressed:(id)sender atIndexPath:(NSIndexPath *)indexPath;
)。 - CrimsonChris