CollectionView的cell长按事件实现
Posted modentime
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于CollectionView的cell长按事件实现的主要内容,如果未能解决你的问题,请参考以下文章
UICollectionView可拖动,并且某个cell不能动
为啥 Collection Cell 不显示在 collectionView 上?
Swift 4.0iOS 11 UICollectionView 长按拖拽删除崩溃的问题