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];
}
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")
}
}
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];
}