iphone - IOS:长按选择UICollectionView Cell

标签 iphone ios uicollectionview

我正在使用 UICollectionView 生成图片库。我在 UICollectionView 单元格中使用了 UIImage 来加载图像。我需要通过长按(而不是单击)选择 UICollectionView Cell。

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

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

关于iphone - IOS:长按选择UICollectionView Cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18224384/

相关文章:

ios - scrollView 的 ContentInset

iphone - 我如何在后台使用 nstimer

ios - 如何修改 iPhone plist 以添加另一个级别并在代码中访问数据

ios - 计算 iPhone 移动的短垂直距离

ios - Smaato 和 iAds 相结合

ios - 手动创建的 UIWindow 大小错误

iphone - 从 Collection View 到详细 View 问题

ios - 无法调整 Collection View 高度

ios - 第二个选项卡中的 UICollectionView 导致第一个选项卡中的 UICollectionView 崩溃应用程序

iPhone UIWebView epubs 逐页拆分