uitableviewcell添加长按手势 并获取cell

来源:互联网 发布:各种算法时间复杂度 编辑:程序博客网 时间:2024/05/23 23:04

http://mobile.51cto.com/iphone-403791.htm

  1. UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];   
  2.     lpgr.minimumPressDuration = 1.0; //seconds  设置响应时间  
  3.     lpgr.delegate = self;     
  4.     [mTableView addGestureRecognizer:lpgr]; //启用长按事件  
  5.     [lpgr release];  
  6.   
  7. -(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer  //长按响应函数  
  8. {  
  9. CGPoint p = [gestureRecognizer locationInView:mTableView ];  
  10.   
  11. //if(gestureRecognizer.state == UIGestureRecognizerStateBegan)  
  12. //{  
  13. //NSLog(@"UIGestureRecognizerStateBegan");  
  14. //}  
  15. //else if(gestureRecognizer.state == UIGestureRecognizerStateEnded)  
  16. //{  
  17. //NSLog(@"UIGestureRecognizerStateEnded");  
  18. //}  
  19. //else if(gestureRecognizer.state == UIGestureRecognizerStateChanged)  
  20. //{  
  21. //NSLog(@"UIGestureRecognizerStateChanged");  
  22. //}  
  23. //else if(gestureRecognizer.state == UIGestureRecognizerStateCancelled)  
  24. //{  
  25. //NSLog(@"UIGestureRecognizerStateCancelled");  
  26. //}  
  27. //else if(gestureRecognizer.state ==UIGestureRecognizerStateFailed )  
  28. //{  
  29. //NSLog(@"UIGestureRecognizerStateFailed");  
  30. //}  
  31.   
  32.   
  33. NSIndexPath *indexPath = [mTableview indexPathForRowAtPoint:p];//获取响应的长按的indexpath  
  34. if (indexPath == nil)  
  35. NSLog(@"long press on table view but not on a row");  
  36. else  
  37. NSLog(@"long press on table view at row %d", indexPath.row);  
  38.   

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

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

添加手势代码:

  1. UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];   
  2.      longPressGr.minimumPressDuration = 1.0;   
  3.      [self.tableView addGestureRecognizer:longPressGr];   
  4.     [longPressGr release];  

响应长按事件代码:

  1. -(void)longPressToDo:(UILongPressGestureRecognizer *)gesture   
  2.  {   
  3.      if(gesture.state == UIGestureRecognizerStateBegan)   
  4.      {   
  5.          CGPoint point = [gesture locationInView:self.tableView];   
  6.         NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point];   
  7.          if(indexPath == nil) return ;   
  8.         //add your code here   
  9.     }   
  10.  }  

0 0
原创粉丝点击