IOS:长按选择UICollectionView单元格

Cha*_*aba 8 iphone ios uicollectionview

我正在使用UICollectionView生成图像库.我UIImageUICollectionViewCell 内部使用来加载图像.我需要选择UICollectionViewCell by Long Press(不是单击).

- (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];

}
Run Code Online (Sandbox Code Playgroud)

Nik*_*Kov 7

斯威夫特4

更新了它的答案

{
    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")
    }
}
Run Code Online (Sandbox Code Playgroud)