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

这里我们为tableview添加长按手势

UILongPressGestureRecognizer *longPressGr = [[UILongPressGestureRecognizer
alloc] initWithTarget:self
action:@selector(longPressAction:)];

longPressGr.minimumPressDuration =
0.5f;

longPressGr.numberOfTouchesRequired =
1;

[_tableView addGestureRecognizer:longPressGr];

[longPressGr release];

这时我们会发现每次按住tableView并且松开的时候, longPressAction: 这个方法会执行2次

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress

{

if (longPress.state ==
UIGestureRecognizerStateBegan) {

CGPoint point = [longPress
locationInView:_tableView];

NSIndexPath *indexPath = [_tableView
indexPathForRowAtPoint:point]; // 可以获取我们在哪个cell上长按

if (indexPath != nil) {

NSLog(@"%ld", indexPath.row);

}

}

}

时间: 2024-08-04 17:02:42

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

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

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

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

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

iOS 在TableView的Cell之间设置空白间隔空间

1.设置section的数目,即是你有多少个cell - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; // in your case, there are 3 cells } 2.对于每个section返回一个cell - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)secti

UICollectionView添加长按手势,长按选中某一个item

很多时候,我们都需要在项目中添加长按手势,比如UICollectionView中,我们长按对某一个item进行删除,那么这时,我们就需要在集合试图中添加长按的手势,手势的添加是简单的,但是添加过手势之后,我们怎么区分我们长按选中的是哪一个item呢 首先,我们先来看看我们是如何添加长按手势的 1.创建集合试图,这个就比较简单了.创建完集合试图,我们在集合试图上面添加长按的手势 UIGestureRecognizerDelegate 先遵从协议 longPressGr = [[UILongPres

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

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

iOS实现TableView中Cell出现时弹出动画

发现一个简单的方式可以让TableView变得非常的炫酷,语言描述太苍白,直接看图吧: 在任何有cell先出现在屏幕上的时候都会有这么一个效果,非常的流畅,也非常有意思(忍不住不停地把玩..).实现起来也非常简单,iOS原生支持,几行代码就可以搞定,在众多的tableview代理方法中,我们利用下面这个方法: -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtInde

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

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

iOS 在tableview的cell中的button上,添加选中状态的解答

大家都知道tableview的复用当然不知道的话可以个我留言或者在网上找  在这我就不多说了: 红色就是选中状态,但是这时候我们会发现往下拉当cell消失后出来新的cell中的button也是选中状态.话不多说下面上解决方法的代码! -(NSMutableArray *)boolArr{ //创建一个数组在这里数组中的NSNumber对象的下标是于 indexPath一一对应的这里我给他一百个根据自身的情况赋值 if (_boolArr==nil) { NSMutableArray *arr =

ios中tableview的移动添加删除

// // MJViewController.m // UITableView-编辑模式 // // Created by mj on 13-4-11. // Copyright (c) 2013年 itcast. All rights reserved. // #import "MJViewController.h" @interface MJViewController () { // 当前的编辑模式 UITableViewCellEditingStyle _editingStyl