ios開發-給cell添加長按手勢


業務需要給cell添加一個長按手勢

//需要在這個方法里添加
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

 //添加長按手勢
    UILongPressGestureRecognizer * longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(cellLongPress:)];
    
    longPressGesture.minimumPressDuration=1.5f;//設置長按 時間
    [cell addGestureRecognizer:longPressGesture];

 return cell;
}
 -(void)cellLongPress:(UILongPressGestureRecognizer *)longRecognizer{
    
    
    if (longRecognizer.state==UIGestureRecognizerStateBegan) {
      //成為第一響應者,需重寫該方法
        [self becomeFirstResponder];

     CGPoint location = [longRecognizer locationInView:self.tableView];
        NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:location];
//可以得到此時你點擊的哪一行

   //在此添加你想要完成的功能

}


}
#pragma mark  實現成為第一響應者方法
-(BOOL)canBecomeFirstResponder{
    return YES;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM