mapanguan

导航

iOS处理视图上同时添加单击与双击手势的冲突问题

    _bgView.userInteractionEnabled = YES;
    //在cell上添加 bgView,给bgView添加两个手势检测方法
    UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];
    doubleTapGesture.numberOfTapsRequired =2;
    //    doubleTapGesture.numberOfTouchesRequired =1;
    [_bgView addGestureRecognizer:doubleTapGesture];
    
    
    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];
    singleTapGesture.numberOfTapsRequired =1;
    //    singleTapGesture.numberOfTouchesRequired  =1;
    [_bgView addGestureRecognizer:singleTapGesture];
    
    
    //只有当doubleTapGesture识别失败的时候(即识别出这不是双击操作),singleTapGesture才能开始识别
    [singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
//两个手势分别响应的方法
-(void)handleSingleTap:(UIGestureRecognizer *)sender{
    NSLog(@"单击");
    
}

-(void)handleDoubleTap:(UIGestureRecognizer *)sender{
    NSLog(@"双击");
}

如果是在cell上添加的视图,则可能与cell点击事件发生冲突,解决办法如下

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 
    TableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.bgView.gestureRecognizers.firstObject.numberOfTouches) {
        NSLog(@"手势响应了");
        return;
    }
    NSLog(@"cell点击");
}

 

posted on 2018-06-21 15:12  mapanguan  阅读(743)  评论(0编辑  收藏  举报