zoukankan      html  css  js  c++  java
  • UICollectionView添加长按手势,长按选中某一个item

    很多时候,我们都需要在项目中添加长按手势,比如UICollectionView中,我们长按对某一个item进行删除,那么这时,我们就需要在集合试图中添加长按的手势,手势的添加是简单的,但是添加过手势之后,我们怎么区分我们长按选中的是哪一个item呢

    首先,我们先来看看我们是如何添加长按手势的

    1.创建集合试图,这个就比较简单了.创建完集合试图,我们在集合试图上面添加长按的手势

    UIGestureRecognizerDelegate 先遵从协议

    longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];
        longPressGr.minimumPressDuration = 1.0;
        longPressGr.delegate = self;
        longPressGr.delaysTouchesBegan = YES;
        [_myCollectionView addGestureRecognizer:longPressGr];

    2.我们在longpressToDo里面添加方法

    -(void)longPressToDo:(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
        }
    }

    在else里面我们根据indexpath对不同的item进行区分,这样是不是很简单

  • 相关阅读:
    [LeetCode][SQL]Rising Temperature
    google API的.NET库
    Google Reader的另一个开源的替代品Go Read
    C#中反射接受的字符串需要满足的Backus-Naur Form语法
    Windows的应用管理工具 PortableApps,Chocolatey和Ninite
    如何定制Windows系统右键菜单
    另一个有趣的Captcha 网站
    .gitignore模板
    遇到sql server的问题时如何排查
    如何传播你的代码
  • 原文地址:https://www.cnblogs.com/nsjelly/p/4629584.html
Copyright © 2011-2022 走看看