ios 手勢傳值

UILongPressGestureRecognizer *longGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(editPathAction:)];
    longGestureRecognizer.minimumPressDuration = 0.7;
    longGestureRecognizer.numberOfTouchesRequired = 1;
    longGestureRecognizer.view.tag = indexPath.row;
    cell.contentView.tag = indexPath.row;
    [cell.contentView addGestureRecognizer:longGestureRecognizer];

在 tableViewCell 上添加了一個長按手勢,在手勢方法 editPathAction: 中需要確認是哪一個cell觸發(fā)了方法,cell.contentView.tag = indexPath.row 給cell一個 tag 值,在 editPathAction: 方法中獲取cell

- (void)editPathAction:(UILongPressGestureRecognizer *)longGestureRecognizer{
    //長按會兩次觸發(fā)手勢方法,可通過 longGestureRecognizer.state == UIGestureRecognizerStateBegan 判斷來解決該問題
    if (longGestureRecognizer.state == UIGestureRecognizerStateBegan) {
        NSInteger index = longGestureRecognizer.view.tag;
        NSDictionary *dic = _dataSource[index];
    }
}

NSInteger index = longGestureRecognizer.view.tag ,可以獲取到觸發(fā)方法的 cell 的 tag 值,即可確定是哪一個 cell 觸發(fā)了方法,也就能獲取到 cell 的數(shù)據(jù)。
***其中,longGestureRecognizer.view 就是添加手勢的那個 view,此處即是 cell.contentView。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容