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进行区分,这样是不是很简单

时间: 2024-12-24 17:36:24

UICollectionView添加长按手势,长按选中某一个item的相关文章

给button添加长按手势并侦测到此button

1, 添加手势 self.longPressRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];[btn addGestureRecognizer:self.longPressRecognizer]; 2,得到当前执行长点选的button - (void)handleLongPress:(UILongPressGestureRecogni

ios开发-给cell添加长按手势

业务需要给cell添加一个长按手势 //需要在这个方法里添加 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //添加长按手势 UILongPressGestureRecognizer * longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self a

IOS给tableview的cell添加长按手势执行两次(UILongPressGestureRecognizer)

这里我们为tableview添加长按手势 UILongPressGestureRecognizer *longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)]; longPressGr.minimumPressDuration = 0.5f; longPressGr.numberOfTouchesRequired = 1; [_tableV

ios 实现在tableViewCell上面添加长按手势 删除该条cell以及列表后台数据等

自己的代码  需要   把属性更改成自己要使用的 //创建长按手势 在cellForRowAtIndexPath代理方法中 UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpGR:)]; //设定最小的长按时间 按不够这个时间不响应手势 longPressGR.minimumPressDuration = 1

UITableView 长按手势

目录: 你需要什么? 如何做? 如何将其利用至UICollectionView上? 何去何从? 本次的 cookbook-style 教程中介绍如何通过长按手势来移动 table view中的cell,这种操作方式就像苹果自家的天气 App 一样. 你可以直接把本文中的到吗添加到你的工程中,或者将其添加到我为你创建好的 starter project 中,也可以下载本文的完整示例工程. 你需要什么? UILongGestureRecognizer UITableView (可以用 UIColle

手势操作(单击手势,长按手势,策划手势)

1.策划手势操作 // // ViewController.m // 1-28策划手势 // // Created by ma c on 16/1/28. // Copyright © 2016年 bjsxt. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (strong, nonatomic) UISwipeGestureRecognizer *rec

iOS长按手势调用两次解决方法

由于以前没有很细致的研究过长按手势,所以今天使用的时候发现长按手势会调用两次响应事件. 主要原因是长按手势会分别在UIGestureRecognizerStateBegan和UIGestureRecognizerStateEnded状态时调用响应函数 这时就需要在响应事件中增加手势状态的判断,根据具体的应用情况在相应的状态中执行操作. typedefNS_ENUM(NSInteger, UIGestureRecognizerState) { UIGestureRecognizerStatePos

JS案例 - 基于vue的移动端长按手势

================================惯例碎碎念前言================================ 当时首先想到要做长按事件的时候,我想到的是vue内部的自定义指令,毕竟官网里边有这么一句描述: 有的情况下,你仍然需要对普通 DOM 元素进行底层操作,这时候就会用到自定义指令. 但是项目用在app中,因为另一个未知原因的bug,自定义事件躺枪(至今死不瞑目).长按事件被我改成了在初始化时,就直接绑定到需要他的dom上. ================

Web 通信 之 长连接、长轮询(long polling)(转载)

基于HTTP的长连接,是一种通过长轮询方式实现"服务器推"的技术,它弥补了HTTP简单的请求应答模式的不足,极大地增强了程序的实时性和交互性. 一.什么是长连接.长轮询? 用通俗易懂的话来说,就是客户端不停的向服务器发送请求以获取最新的数据信息.这里的“不停”其实是有停止的,只是我们人眼无法分辨是否停止,它只是一种快速的停下然后又立即开始连接而已. 二.长连接.长轮询的应用场景 长连接.长轮询一般应用与WebIM.ChatRoom和一些需要及时交互的网站应用中.其真实案例有:WebQQ