- (void)viewDidLoad
{
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
//设置长按的时间press.minimumPressDuration = 0.5;//2秒(默认0.5秒)
//设置长按的手指数[press setNumberOfTouchesRequired:1];
//把手势加到界面上,可以加到任何控件上,需要开启用户交互
// _lable.userInteractionEnabled = YES;// [self.view addGestureRecognizer:press];
[cell addGestureRecognizer:press];}
//长按后触发该方法
给tableview或者collectionView的cell添加长按手势执行两次(UILongPressGestureRecognizer)开始一次结束一次
-(void)longPressAction:(UILongPressGestureRecogn
{
// 可以获取我们在哪个cell上长按 tableview collectionView
CGPoint point = [longPress locationInView:_collectionView];
//CGPoint point = [longPress locationInView:_tableView];
NSIndexPath *indexPath = [_collectionView indexPathForItemAtPoint:point];
//NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:point];
if (indexPath != nil) {
NSLog(@"%ld", indexPath.row);
}
}