ios - UICollectionViewCell 上的长按手势

标签 ios objective-c uicollectionview

我想知道如何将长按手势识别器添加到 UICollectionView(的子类)。我在文档中读到它是默认添加的,但我不知道如何。

我想做的是: 长按一个单元格( I have a calendar thingy from github ),获取点击了哪个单元格,然后对其进行处理。我需要知道长按的是哪个单元格。抱歉这个广泛的问题,但我在谷歌或 SO 上找不到更好的东西

最佳答案

objective-C

在您的 myCollectionViewController.h 文件中添加 UIGestureRecognizerDelegate 协议(protocol)

@interface myCollectionViewController : UICollectionViewController<UIGestureRecognizerDelegate>

在您的 myCollectionViewController.m 文件中:

- (void)viewDidLoad
{
    // attach long press gesture to collectionView
    UILongPressGestureRecognizer *lpgr 
       = [[UILongPressGestureRecognizer alloc]
                     initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.delegate = self;
    lpgr.delaysTouchesBegan = YES;
    [self.collectionView addGestureRecognizer:lpgr];
}

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
        return;
    }
    CGPoint p = [gestureRecognizer locationInView:self.collectionView];

    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
    if (indexPath == nil){
        NSLog(@"couldn't find index path");            
    } else {
        // get the cell at indexPath (the one you long pressed)
        UICollectionViewCell* cell =
        [self.collectionView cellForItemAtIndexPath:indexPath];
        // do stuff with the cell
    }
}

swift

class Some {

    @objc func handleLongPress(gesture : UILongPressGestureRecognizer!) {
        if gesture.state != .Ended {
            return
        }
        let p = gesture.locationInView(self.collectionView)

        if let indexPath = self.collectionView.indexPathForItemAtPoint(p) {
            // get the cell at indexPath (the one you long pressed)
            let cell = self.collectionView.cellForItemAtIndexPath(indexPath)
            // do stuff with the cell
        } else {
            print("couldn't find index path")
        }
    }
}

let some = Some()
let lpgr = UILongPressGestureRecognizer(target: some, action: #selector(Some.handleLongPress))

swift 4

class Some {

    @objc func handleLongPress(gesture : UILongPressGestureRecognizer!) {
        if gesture.state != .ended { 
            return 
        } 

        let p = gesture.location(in: self.collectionView) 

        if let indexPath = self.collectionView.indexPathForItem(at: p) { 
            // get the cell at indexPath (the one you long pressed) 
            let cell = self.collectionView.cellForItem(at: indexPath) 
            // do stuff with the cell 
        } else { 
            print("couldn't find index path") 
        }
    }
}

let some = Some()
let lpgr = UILongPressGestureRecognizer(target: some, action: #selector(Some.handleLongPress))

关于ios - UICollectionViewCell 上的长按手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18848725/

相关文章:

ios - 使用自定义 Collection View 布局快速配置动态单元格高度

ios - 在大小更改时使 Collection View 布局无效的正确方法

ios - 滑动 View 就像iOS中的whatsapp(android)

ios - 如何获得高质量的 iOS 屏幕截图?

ios - Google Places Autocomplete API 过滤器类型 "address"通过 iOS SDK 返回类型为 "route"的地点

iOS:用异常原因符号化崩溃日志

ios - 视频完成后如何让youtube视频退出全屏

ios - 如何在可变距离处沿向量找到一个点

iphone - 谁能告诉我为什么我的代码无法创建数据库和表,然后在其中存储数据。

iphone - 从 super View 中删除后保持图像在 View 中