【问题标题】:IOS: Selecting UICollectionView Cell by Long PressIOS:通过长按选择 UICollectionView 单元格
【发布时间】:2013-08-14 06:11:23
【问题描述】:

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

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

}

【问题讨论】:

  • 你可以使用UILongPressGestureRecognizer

标签: iphone ios uicollectionview


【解决方案1】:

斯威夫特 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")
    }
}

【讨论】:

  • 什么意思:“如果让 indexPath = indexPath {”
  • 它解开indexPath,即self.collectionView.indexPathForItem(at: point)
【解决方案2】:

您可以使用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];
    }

【讨论】:

    猜你喜欢
    • 2019-10-20
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    相关资源
    最近更新 更多