UITableViewCell只接受longpress

时间:2014-07-28 05:38:47

标签: ios objective-c uitableview uitapgesturerecognizer

遇到UITableViewCell的问题。我试图获取所选UITableViewCell的数据。最初它运作良好,但在UITapGestureRecognizer之后它会造成这个麻烦。我使用的表被分配到一个子视图,即使很少有按钮没有在第一次点击时采取行动。添加UITapGestureRecognizer后,我正面临着这个问题。

tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                            action:@selector(didTapAnywhere:)];
    [tapRecognizer setNumberOfTapsRequired:1];
    [tapRecognizer setNumberOfTouchesRequired:1];
    [self.view addGestureRecognizer:tapRecognizer];
    [self.scrolling addGestureRecognizer:tapRecognizer];
    [self.notesView addGestureRecognizer:tapRecognizer];

-(void)didTapAnywhere: (UITapGestureRecognizer*) recognizer {

    [ageview removeFromSuperview];
    [contiView removeFromSuperview];
    [CountryTableview removeFromSuperview];
    [notesView endEditing:YES];
    [self.view endEditing:YES];
}

2 个答案:

答案 0 :(得分:1)

您的Tap Guest正在取消您的手机和按钮的触摸访客,确保您分配点击访客的视图与按钮不重叠,这将导致取消触摸事件

答案 1 :(得分:0)

您是否实现了UIGestureRecognizerDelegate

的委托方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    UIView *view = (UIView *)touch.view;
    if([view isKindOfClass:[<YOUR_BUTTON> class]]) {
        return NO;
    }

    return YES;
}

或者您可以为按钮提供一些标记,如下所示

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    UIView *view = (UIView *)touch.view;
    if(view.tag == 10000) {
        return NO;
    }

    return YES;
}