您的位置:首页 > 产品设计 > UI/UE

uitableviewcell添加长按手势 并获取cell

2016-03-26 13:59 375 查看
http://mobile.51cto.com/iphone-403791.htm

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];

lpgr.minimumPressDuration = 1.0; //seconds 设置响应时间

lpgr.delegate = self;

[mTableView addGestureRecognizer:lpgr]; //启用长按事件

[lpgr release];

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer //长按响应函数

{

CGPoint p = [gestureRecognizer locationInView:mTableView ];

//if(gestureRecognizer.state == UIGestureRecognizerStateBegan)

//{

//NSLog(@"UIGestureRecognizerStateBegan");

//}

//else if(gestureRecognizer.state == UIGestureRecognizerStateEnded)

//{

//NSLog(@"UIGestureRecognizerStateEnded");

//}

//else if(gestureRecognizer.state == UIGestureRecognizerStateChanged)

//{

//NSLog(@"UIGestureRecognizerStateChanged");

//}

//else if(gestureRecognizer.state == UIGestureRecognizerStateCancelled)

//{

//NSLog(@"UIGestureRecognizerStateCancelled");

//}

//else if(gestureRecognizer.state ==UIGestureRecognizerStateFailed )

//{

//NSLog(@"UIGestureRecognizerStateFailed");

//}

NSIndexPath *indexPath = [mTableview indexPathForRowAtPoint:p];//获取响应的长按的indexpath

if (indexPath == nil)

NSLog(@"long press on table view but not on a row");

else

NSLog(@"long press on table view at row %d", indexPath.row);

}

给UITableView 添加长按手势,识别长按哪一行。

长按手势类UILongPressGestureRecognizer, 属性minimumPressDuration表示最短长按的时间

添加手势代码:
UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];
longPressGr.minimumPressDuration = 1.0;
[self.tableView addGestureRecognizer:longPressGr];
[longPressGr release];


响应长按事件代码:
-(void)longPressToDo:(UILongPressGestureRecognizer *)gesture
{
if(gesture.state == UIGestureRecognizerStateBegan)
{
CGPoint point = [gesture locationInView:self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point];
if(indexPath == nil) return ;
//add your code here
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: