Cell long by event for iOS advanced development--collectionview

Source: Internet
Author: User

When we use TableView, the default is to click or slide the delete operation, but the original is not long-press operation. and came to CollectionView, and less a side-slip operation. In actual project development, we need to use a click or a long press to do different things and get the section and row of the cell. So we do it in CollectionView, which is similar in TableView.

The demo I have uploaded to Https://github.com/chenyufeng1991/CollectionView. It also contains CollectionView's other demos. 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. On the basis of continuing.

(1) The native cell does not have a long press event and we need to use gesture recognition to bind the CollectionView. Create and Bind CollectionView as follows:

-(void) viewdidload {  [super viewdidload];  /*
****
*    ///create long press gesture Monitor  uilongpressgesturerecognizer *longpress = [[Uilongpressgesturerecognizer alloc]                                             Initwithtarget:self                                             Action: @selector (myhandletableviewcelllongpressed:)];  Longpress.minimumpressduration = 1.0;  Add a long-press gesture to a view that needs to implement a long-press operation  [Self.collectionview addgesturerecognizer:longpress];}

(2) Handling long press 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 (@ "empty");    } 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, long press the cell can get the indexpath.section and Indexpath.row value of the cell. Also note that long-press events and click events do not conflict and do not have any relationship with each other, this is the happiest. In this way, we have extended the function of CollectionView.



Recently open source IOS app, high-profile Evernote https://github.com/chenyufeng1991/iOS-Oncenote. Welcome everyone to praise and follow the project progress. You can also install it on your phone and try it on.

GitHub home: https://github.com/chenyufeng1991. Welcome everyone to visit!



Cell long by event for iOS advanced development--collectionview

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.