UITableView 中 didSelectRowAtIndexPath 手势冲突或者双击事件处理

实现双击事件的方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
        NSUInteger current = [[NSDate date] timeIntervalSince1970]*1000;
        if (current - taptime < 500) {
            // TODO
        }
        taptime = current;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    tapCount++;
    switch (tapCount)
    {
        case 1: //single tap
            [self performSelector:@selector(singleTap) withObject: nil afterDelay: .4];
            break;
        case 2: //double tap
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
            [self performSelector:@selector(doubleTap) withObject: nil];
            break;
        default:
            break;
    }
}
- (void)singleTap
{
    tapCount = 0;
    // TODO
}

- (void)doubleTap
{
    tapCount = 0;
    // TODO
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    int count = [touch tapCount];
    if (count == 2) {
        // TODO
        NSLog(@"success");
    }
} 

事件冲突

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if([NSStringFromClass([touch.view class])isEqualToString:@"UITableViewCellContentView"]){
    // TODO

    return NO;
    }
return YES;
}

绑定单击,双击点击手势

// [A requireGestureRecognizerToFail:B]函数,它可以指定当A手势发生时,即便A已经滿足条件了,也不会立刻触发,会等到指定的手势B确定失败之后才触发。

- (void)viewDidLoad 
{
    // 单击的 Recognizer
    UITapGestureRecognizer* singleRecognizer;
    singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(SingleTap:)];
    //点击的次数
    singleTapRecognizer.numberOfTapsRequired = 1; // 单击
    //给self.view添加一个手势监测;
  [self.view addGestureRecognizer:singleRecognizer];

    // 双击的 Recognizer
    UITapGestureRecognizer* doubleRecognizer;
    doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(DoubleTap:)];
    doubleTapRecognizer.numberOfTapsRequired = 2; // 双击
    //关键语句,给self.view添加一个手势监测;
    [self.view addGestureRecognizer:doubleRecognizer];

    // 关键在这一行,双击手势确定监测失败才会触发单击手势的相应操作
    [singleRecognizer requireGestureRecognizerToFail:doubleRecognizer];
}

-(void)SingleTap:(UITapGestureRecognizer*)recognizer{
// TODO
}

-(void)DoubleTap:(UITapGestureRecognizer*)recognizer{
// TODO
}

评论

h
hw_7864

⭐️ Admin快速开发: https://show.cool-admin.com/

赞 0
h
hw_7864

⭐️ 官方文档: https://cool-js.com

赞 0
h
hw_7864

⭐️ chatgpt在线体验: https://cool-js.com/ai/chat-vip/index.html?r=helloworld

赞 0