Long press gesture on UICollectionViewCell
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))
The same code @abbood's code for Swift:
In viewDidLoad:
let lpgr : UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")lpgr.minimumPressDuration = 0.5lpgr.delegate = selflpgr.delaysTouchesBegan = trueself.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
Use the delegate of UICollectionView receive long press event
You must impl 3 method below.
//UICollectionView menu delegate- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath{ //Do something return YES;}- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender{ //do nothing return NO;}- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender{ //do nothing}