CollectionView的cell长按事件实现
原生cell没有长按事件,我们需要使用手势识别来绑定CollectionView。创建并绑定CollectionView如下:
(void)viewDidLoad { [super viewDidLoad];
*/ //创建长按手势监听 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(myHandleTableviewCellLongPressed:)]; longPress.minimumPressDuration = 1.0; //将长按手势添加到需要实现长按操作的视图里 [self.collectionView addGestureRecognizer:longPress];
处理长按事件:
(void) myHandleTableviewCellLongPressed:(UILongPressGestureRecognizer *)gestureRecognizer { CGPoint pointTouch = [gestureRecognizer locationInView:self.collectionView]; if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { NSLog(@"UIGestureRecognizerStateBegan"); NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:pointTouch]; if (indexPath == nil) { NSLog(@"空"); }else{ NSLog(@"Section = %ld,Row = %ld",(long)indexPath.section,(long)indexPath.row); } } if (gestureRecognizer.state == UIGestureRecognizerStateChanged) { NSLog(@"UIGestureRecognizerStateChanged"); } if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { NSLog(@"UIGestureRecognizerStateEnded"); } }
原文: https://blog.csdn.net/CHENYUFENG1991/article/details/50037361