IOS advanced development -- cell long-press event Implementation of CollectionView

Source: Internet
Author: User

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.

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.