UITableView 添加長按手勢UILongPressGestureRecognizer

 給UITableView 添加長按手勢,識別長按哪一行。

    長按手勢類UILongPressGestureRecognizer, 屬性minimumPressDuration表示最短長按的時間

   添加手勢代碼:


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

    longPressGr.minimumPressDuration = 1.0;

    [self.tableView addGestureRecognizer:longPressGr];

 


響應長按事件代碼:

-(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
         
 
         
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章