ste*_*rro 1 uitableview ios uitapgesturerecognizer
我在UITableViewCells中添加了单击和双击手势识别器.但是在滚动表格几次之后,在我的滑动手势结束以滚动表格和滚动动画的开始之间会出现越来越长的暂停.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"tableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[cell addGestureRecognizer:singleTap];
UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.numberOfTouchesRequired = 1;
[singleTap requireGestureRecognizerToFail:doubleTap];
[cell addGestureRecognizer:doubleTap];
if (tableView == self.searchDisplayController.searchResultsTableView) {
// search results
}
else {
// normal table
}
return cell;
}
Run Code Online (Sandbox Code Playgroud)
SingleTap:和doubleTap:方法
- (void)singleTap:(UITapGestureRecognizer *)tap
{
if (UIGestureRecognizerStateEnded == tap.state) {
UITableViewCell *cell = (UITableViewCell *)tap.view;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath* indexPath = [tableView indexPathForCell:cell];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
// do single tap
}
}
- (void)doubleTap:(UITapGestureRecognizer *)tap
{
if (UIGestureRecognizerStateEnded == tap.state) {
UITableViewCell *cell = (UITableViewCell *)tap.view;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath* indexPath = [tableView indexPathForCell:cell];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
// do double tap
}
}
Run Code Online (Sandbox Code Playgroud)
由于初始滚动是平滑的,我尝试将手势识别器添加到if (cell == nil)条件中,但随后它们从未添加到单元格中.
我最初也将手势添加到tableView而不是单个单元格,但这会导致searchDisplayController出现问题,即点击取消按钮无法识别.
我会批评任何想法,谢谢.
对同一个NSIndexPath多次调用cellForRowAtIndexPath方法,因此您向单元格添加了太多的手势识别器.因此,性能将受到影响.
我的第一个建议是在表格视图中只添加一个手势识别器.(我写了一个类似问题的答案:https://stackoverflow.com/a/4604667/550177 )
但正如你所说,它会导致searchDisplayController出现问题.也许你可以通过UIGestureRecognizerDelegate的智能实现来避免它们(-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;当点击不在单元格内时返回NO ).
我的第二个建议:只添加一次手势识别器:
if ([cell.gestureRecognizers count] == 0) {
// add recognizer for single tap + double tap
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2104 次 |
| 最近记录: |