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

我想做的是:

推荐答案

Objective-C

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

@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相关问答推荐

自定义油漆圆角三角形

如何通过S定义的对象设置删除基于块的观察者的测试?

在视图上执行手势会阻止相机操作

如何使用参与者允许并行读取,但阻止对SWIFT中资源的并发读取和写入?

SwiftUI-工作表不显示导航标题

为什么@FetchRequest 在删除时不更新和重绘视图?

UIControl 子类 - 按下时显示 UIMenu

iOS SwiftUI - 使用 PhotosPicker 从图库中 Select 图像或视频

iOS 16 堆栈视图中按钮的奇怪动画

SwiftUI DatePicker 格式在每个设备上都不一样,如何让它总是显示相同的 Select 版本?

使用 CIImage 支持的 UIImage 设置 UIImageView 时发生罕见的崩溃

Apple Push Service 证书不受信任

Xcode 4.1 致命错误:自构建预编译头文件后修改了 stdlib

UIView 动画与 CALayers

在 Swift 中上传带参数的图像

如何在 OS X 或 iOS(不使用格式塔)中确定运行时的操作系统版本?

Swift:调用中缺少参数标签xxx

使用 Xcode 4 的 iPhone 临时构建

Sierra 中的安全/协同设计: keys 串忽略访问控制设置和 UI 提示权限

iOS UITextView 或 UILabel 带有可点击的动作链接