Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS: Selecting UICollectionView Cell by Long Press

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:@"[email protected]"];
    [cell addSubview:OverlayImage];

}
like image 459
Chanuka Ranaba Avatar asked Aug 14 '13 06:08

Chanuka Ranaba


1 Answers

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")
    }
}
like image 189
Nik Kov Avatar answered Oct 14 '22 08:10

Nik Kov