Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long press gesture on UICollectionViewCell

I was wondering how to add a long press gesture recognizer to a (subclass of) UICollectionView. I read in the documentation that it is added by default, but I can't figure out how.

What I want to do is: Long press on a cell ( I have a calendar thingy from github ), get which cell is tapped and then do stuff with it. I need to know what cell is longpressed. Sorry for this broad question, but i couldn't find anything better on either google or SO

like image 467
Oscar Apeland Avatar asked Sep 17 '13 11:09

Oscar Apeland


People also ask

How do you use UILongPressGestureRecognizer?

UILongPressGestureRecognizer is a concrete subclass of UIGestureRecognizer . The user must press one or more fingers on a view and hold them there for a minimum period of time before the action triggers.

What is UITapGestureRecognizer Swift?

UITapGestureRecognizer is a concrete subclass of UIGestureRecognizer . For gesture recognition, the specified number of fingers must tap the view a specified number of times. Although taps are discrete gestures, they're discrete for each state of the gesture recognizer.

What is gesture in Swift?

The tap gesture recognizer appears in the Document Outline on the left. We need to implement an action that defines what happens when the user taps the image view. Open ViewController. swift and define a method with name didTapImageView(_:) . It accepts the tap gesture recognizer as its only argument.


2 Answers

Objective-C

In your myCollectionViewController.h file add the UIGestureRecognizerDelegate protocol

@interface myCollectionViewController : UICollectionViewController<UIGestureRecognizerDelegate> 

in your myCollectionViewController.m file:

- (void)viewDidLoad {     // attach long press gesture to collectionView     UILongPressGestureRecognizer *lpgr         = [[UILongPressGestureRecognizer alloc]                      initWithTarget:self action:@selector(handleLongPress:)];     lpgr.delegate = self;     lpgr.delaysTouchesBegan = YES;     [self.collectionView addGestureRecognizer:lpgr]; }  -(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {     if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {         return;     }     CGPoint p = [gestureRecognizer locationInView:self.collectionView];      NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];     if (indexPath == nil){         NSLog(@"couldn't find index path");                 } else {         // get the cell at indexPath (the one you long pressed)         UICollectionViewCell* cell =         [self.collectionView cellForItemAtIndexPath:indexPath];         // do stuff with the cell     } } 

Swift

class Some {      @objc func handleLongPress(gesture : UILongPressGestureRecognizer!) {         if gesture.state != .Ended {             return         }         let p = gesture.locationInView(self.collectionView)          if let indexPath = self.collectionView.indexPathForItemAtPoint(p) {             // get the cell at indexPath (the one you long pressed)             let cell = self.collectionView.cellForItemAtIndexPath(indexPath)             // do stuff with the cell         } else {             print("couldn't find index path")         }     } }  let some = Some() let lpgr = UILongPressGestureRecognizer(target: some, action: #selector(Some.handleLongPress)) 

Swift 4

class Some {      @objc func handleLongPress(gesture : UILongPressGestureRecognizer!) {         if gesture.state != .ended {              return          }           let p = gesture.location(in: self.collectionView)           if let indexPath = self.collectionView.indexPathForItem(at: p) {              // get the cell at indexPath (the one you long pressed)              let cell = self.collectionView.cellForItem(at: indexPath)              // do stuff with the cell          } else {              print("couldn't find index path")          }     } }  let some = Some() let lpgr = UILongPressGestureRecognizer(target: some, action: #selector(Some.handleLongPress)) 
like image 112
abbood Avatar answered Oct 24 '22 05:10

abbood


The same code @abbood's code for Swift:

In viewDidLoad:

let lpgr : UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPress:") lpgr.minimumPressDuration = 0.5 lpgr.delegate = self lpgr.delaysTouchesBegan = true self.collectionView?.addGestureRecognizer(lpgr) 

And the function:

func handleLongPress(gestureRecognizer : UILongPressGestureRecognizer){      if (gestureRecognizer.state != UIGestureRecognizerState.Ended){         return     }      let p = gestureRecognizer.locationInView(self.collectionView)      if let indexPath : NSIndexPath = (self.collectionView?.indexPathForItemAtPoint(p))!{         //do whatever you need to do     }  } 

Do not forget the delegate UIGestureRecognizerDelegate

like image 36
Guilherme de Freitas Avatar answered Oct 24 '22 05:10

Guilherme de Freitas