IOS advanced development -- cell long-press event Implementation of CollectionView
When we use TableView, operations such as clicking or sliding Delete are performed by default, but native operations such as long-clicking are not performed. In CollectionView, a slide operation is missing. In actual project development, we need to use a single click or a long press to perform different operations and obtain the section and row of the cell. So we implement this in CollectionView, which is similar to TableView.
This demo I have uploaded to https://github.com/chenyufeng1991/CollectionView. It also contains other demos of CollectionView. I will be in https://github.com/chenyufeng1991/CollectionView/tree/master/CollectionView%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94%E8%8E%B7%E5%8F%96Cell%E4%B8%AD%E6%8C%89%E9%92% AE %E7%82%B9%E5%87%BB%E4%BA%8B%E4%BB%B6. And then proceed.
(1) Native cells do not have long-pressed events. We need to use Gesture Recognition to bind CollectionView. Create and bind a CollectionView as follows:
- (void)viewDidLoad { [super viewDidLoad]; /*
****
* // Create a long-pressed gesture listener UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (myHandleTableviewCellLongPressed :)]; longPress. minimumPressDuration = 1.0; // Add the long-pressed gesture to the view for implementing the long-pressed operation [self. collectionView addGestureRecognizer: longPress];}
(2) handling long-cycle events:
-(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 (@ blank);} else {NSLog (@ Section = % ld, Row = % ld, (long) indexPath. section, (long) indexPath. row) ;}} if (gestureRecognizer. state = UIGestureRecognizerStateChanged) {NSLog (@ UIGestureRecognizerStateChanged);} if (gestureRecognizer. state = UIGestureRecognizerStateEnded) {NSLog (@ UIGestureRecognizerStateEnded );}}
(3) run the program and press the cell to obtain the indexPath. section and indexPath. row values of the cell. Note that long-press events and click events do not conflict with each other and do not have any relationship with each other. This is the happiest thing. In this way, the functions of CollectionView are extended.