我使用以下代码将UIPanGuestureRecognizer添加到整个视图中:
UIPanGestureRecognizer *pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; [[self view] addGestureRecognizer:pgr];
在主视图中我有一个UITableView,它有这个代码来启用滑动删除功能:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"RUNNING2"); return UITableViewCellEditingStyleDelete; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row >= _firstEditableCell && _firstEditableCell != -1) NSLog(@"RUNNING1"); return YES; else return NO; }
只有RUNNING1打印到日志中,并且“删除”按钮不会显示.我相信其原因是UIPanGestureRecognizer,但我不确定.如果这是正确的,我该如何解决这个问题.如果这不正确,请提供原因并解决.谢谢.
解决方法
从
document:
If a gesture recognizer recognizes its gesture,the remaining touches for the view are cancelled.
您的UIPanGestureRecognizer会首先识别滑动手势,因此您的UITableView不再接收触摸.
要使表格视图与手势识别器同时接收触摸,请将其添加到手势识别器的委托:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }