ios给cell添加长按手势怎么识别是哪个cell

ios给cell添加长按手势怎么识别是哪个cell,第1张

这里我们为tableview添加长按手势

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

longPressGr.minimumPressDuration = 0.5f

longPressGr.numberOfTouchesRequired = 1

[_tableView addGestureRecognizer:longPressGr]

[longPressGr release]

这时我们会发现每次按住tableView并且松开的时候, longPressAction: 这个方法会执行2次

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress

{

if (longPress.state == UIGestureRecognizerStateBegan) {

CGPoint point = [longPress locationInView:_tableView]

NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:point]// 可以获取我们在哪个cell上长按

if (indexPath != nil) {

NSLog(@"%ld", indexPath.row)

}

}

}

自定义方法:

UIMenuItem *share = [[UIMenuItem alloc] initWithTitle:@"分享"action:@selector(shareClick:)]

UIMenuItem *report = [[UIMenuItem alloc] initWithTitle:@"举报"action:@selector(report:)]

UIMenuController *menu = [UIMenuController sharedMenuController]

[menu setMenuItems:[NSArray arrayWithObjects:share, report, nil]]

[menu setTargetRect:cell.c_post_content.frame inView:cell.c_post_content.superview]

[menu setMenuVisible:YES animated:YES]


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11692929.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存