9

UICollectionView用来生成图片库。我UIImage在 Cell 内使用UICollectionView来加载图像。我需要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];

}
4

2 回答 2

10

斯威夫特 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")
    }
}
于 2018-04-07T10:42:06.513 回答
0

您可以使用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];
    }
于 2013-08-14T06:15:54.973 回答