iOS UITableView添加長按事件 —— HERO博客

爲UITableView添加長按事件,長按cell時獲取到當前位置:

1. 爲UITableView添加長按手勢識別器

    //添加長按事件
    UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongPressTableViewCell:)];
    gesture.minimumPressDuration = 1.0;
    [self.tableView addGestureRecognizer:gesture];

2. 實現長按事件方法

//tableViewCell長按事件
- (void)didLongPressTableViewCell:(UILongPressGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        //獲取到點擊的位置
        CGPoint point = [gesture locationInView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
        if(indexPath == nil) return;
        
        //do something...
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章