9

I'm using UICollectionView to generate a image gallery.I used UIImage inside the UICollectionView Cell to load the images.I need to select UICollectionView Cell by Long Press (not by single tap).

- (IBAction)longPress:(UILongPressGestureRecognizer *)gestureRecognizer
{

    UICollectionViewCell *cell=(UICollectionViewCell *)[gestureRecognizer view];
    int index=cell.tag;

    OverlayImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width,     cell.frame.size.height)];
    OverlayImage.image = [UIImage imageNamed:@"Overlay@2x.png"];
    [cell addSubview:OverlayImage];

}
beryllium
  • 29,214
  • 15
  • 99
  • 123
Chanuka Ranaba
  • 529
  • 2
  • 10
  • 22

2 Answers2

10

Swift 4

updated answer of it

{
    let longPressGR = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(longPressGR:)))
    longPressGR.minimumPressDuration = 0.5
    longPressGR.delaysTouchesBegan = true
    self.collectionView.addGestureRecognizer(longPressGR)
}

@objc
func handleLongPress(longPressGR: UILongPressGestureRecognizer) {
    if longPressGR.state != .ended {
        return
    }
    
    let point = longPressGR.location(in: self.collectionView)
    let indexPath = self.collectionView.indexPathForItem(at: point)
    
    if let indexPath = indexPath {
        var cell = self.collectionView.cellForItem(at: indexPath)
        print(indexPath.row)
    } else {
        print("Could not find index path")
    }
}
Community
  • 1
  • 1
Nik Kov
  • 10,605
  • 4
  • 56
  • 96
0

You can use LongPressGesture

UILongPressGestureRecognizer *longpressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
    longpressGesture.minimumPressDuration = 5;
    [longpressGesture setDelegate:self];
    [self.yourImage addGestureRecognizer:longpressGesture];


    - (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
        NSLog(@"longPressHandler");
        UIImageView *tempImage=(UIImageView*)[gestureRecognizer view];
    }
Toseef Khilji
  • 16,458
  • 10
  • 78
  • 115