在UICollectionViewCell上长按手势

Osc*_*and 104 objective-c ios uicollectionview

我想知道如何将长按手势识别器添加到UICollectionView的(子类).我在文档中读到它默认添加,但我无法弄清楚如何.

我想要做的是:长按一个单元格(我有一个来自github的日历),得到哪个单元格,然后用它做东西.我需要知道什么细胞是长按的.抱歉这个广泛的问题,但我无论是谷歌还是SO都找不到更好的东西

abb*_*ood 212

Objective-C的

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

@interface myCollectionViewController : UICollectionViewController<UIGestureRecognizerDelegate>
Run Code Online (Sandbox Code Playgroud)

在你的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
    }
}
Run Code Online (Sandbox Code Playgroud)

迅速

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))
Run Code Online (Sandbox Code Playgroud)

斯威夫特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))
Run Code Online (Sandbox Code Playgroud)

  • 对于(至少)ios7,你必须添加`lpgr.delaysTouchesBegan = YES;`以避免首先触发`didHighlightItemAtIndexPath`. (9认同)
  • 为什么要添加`lpgr.delegate = self;`?它没有委托就可以正常工作,你也没有提供. (7认同)
  • @abbood答案是有效的,但是当长按识别器处于活动状态时,我无法在收集视图中上下滚动(使用另一根手指).是什么赋予了? (3认同)
  • 就个人而言,我会做`UIGestureRecognizerStateBegan`,因此手势在识别时使用,而不是在用户松开手指时使用. (3认同)

Gui*_*tas 25

同样的代码@ abbood的Swift代码:

在viewDidLoad中:

let lpgr : UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
lpgr.minimumPressDuration = 0.5
lpgr.delegate = self
lpgr.delaysTouchesBegan = true
self.collectionView?.addGestureRecognizer(lpgr)
Run Code Online (Sandbox Code Playgroud)

功能:

func handleLongPress(gestureRecognizer : UILongPressGestureRecognizer){

    if (gestureRecognizer.state != UIGestureRecognizerState.Ended){
        return
    }

    let p = gestureRecognizer.locationInView(self.collectionView)

    if let indexPath : NSIndexPath = (self.collectionView?.indexPathForItemAtPoint(p))!{
        //do whatever you need to do
    }

}
Run Code Online (Sandbox Code Playgroud)

不要忘记代表 UIGestureRecognizerDelegate

  • 运行良好,但是如果你想让代码在最小持续时间过去后触发,而不仅仅是当用户拿起他/她的手指时,请将`UIGestureRecognizerState.Ended`更改为`UIGestureRecognizerState.Began`. (12认同)
  • 工作得很好,只需注意"handleLongPress:"应更改为#selector(YourViewController.handleLongPress(_ :)) (3认同)
  • 代理不需要代码,也不在代码中使用. (3认同)

Ren*_*dro 12

斯威夫特 5:

private func setupLongGestureRecognizerOnCollection() {
    let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(gestureRecognizer:)))
    longPressedGesture.minimumPressDuration = 0.5
    longPressedGesture.delegate = self
    longPressedGesture.delaysTouchesBegan = true
    collectionView?.addGestureRecognizer(longPressedGesture)
}

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

    let p = gestureRecognizer.location(in: collectionView)

    if let indexPath = collectionView?.indexPathForItem(at: p) {
        print("Long press at item: \(indexPath.row)")
    }
}
Run Code Online (Sandbox Code Playgroud)

另外不要忘记实现 UIGestureRecognizerDelegate 并从 viewDidLoad 或任何你需要调用它的地方调用 setupLongGestureRecognizerOnCollection 。


liu*_*ing 10

使用UICollectionView的委托接收长按事件

你必须在下面提出3种方法.

//UICollectionView menu delegate
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath{

   //Do something

   return YES;
}
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender{
    //do nothing
    return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender{
    //do nothing
}
Run Code Online (Sandbox Code Playgroud)


tig*_*ero 8

这里添加自定义longpress手势识别器的答案是正确的,但是根据此处的文档:类的父类UICollectionView安装a default long-press gesture recognizer来处理滚动交互,因此您必须将自定义点击手势识别器链接到与您的集合视图关联的默认识别器.

以下代码将避免您的自定义手势识别器干扰默认手势识别器:

UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];

longPressGesture.minimumPressDuration = .5; //seconds
longPressGesture.delegate = self;

// Make the default gesture recognizer wait until the custom one fails.
for (UIGestureRecognizer* aRecognizer in [self.collectionView gestureRecognizers]) {
   if ([aRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
      [aRecognizer requireGestureRecognizerToFail:longPressGesture];
} 
Run Code Online (Sandbox Code Playgroud)