0

我有UIView一个UITapGestureRecognizer附件,当用户在UITextFields. 现在,我还有一些标签,当点击时会显示UIPickerView. 标签UITapGestureRecognizer也使用 an 。问题是这些事件似乎在蚕食自己。

点击我的标签时是否可以执行两个事件处理程序?

谢谢你。

UITapGestureRecognizer* tapForUnit = [[UITapGestureRecognizer alloc] initWithTarget:self.fridgeItemUnit action:@selector(onTap)];
[self.fridgeItemUnit addGestureRecognizer:tapForUnit];

上面的代码是针对其中一个标签的。我删除了视图的代码,因为我的标签将停止工作,但它完全相同,唯一不同的是它附加到self.view并且执行的函数是这个:

-(void)dismissKeyboard:(UIGestureRecognizer*)gesture {
    [self.fridgeItemName resignFirstResponder];
    [self.fridgeItemQuantity resignFirstResponder];
}
4

1 回答 1

2

我将从以下方法实现UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

我附上一个例子:

截屏

我在 XIB 上做的唯一一件事就是启用用户交互。这里.mUIViewController

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    _viewRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTap:)];
    [_viewRecognizer setDelegate:self];
    _labelRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];
    [_labelRecognizer setDelegate:self];
    [self.view addGestureRecognizer:_viewRecognizer];
    [self.label addGestureRecognizer:_labelRecognizer];
}

- (void)viewDidUnload {

    [super viewDidUnload];
    [_viewRecognizer release]; _viewRecognizer = nil;
    [_labelRecognizer release]; _labelRecognizer = nil;
    self.label = nil;
}

- (void)dealloc {
    [_viewRecognizer release];
    [_labelRecognizer release];
    self.label = nil;
    [super dealloc];
}

- (void)labelTap:(UIGestureRecognizer *)recognizer {

    NSLog(@"labelTap");
}

- (void)viewTap:(UIGestureRecognizer *)recognizer {

    NSLog(@"viewTap");
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    NSLog(@"shouldRecognizeSimultaneouslyWithGestureRecognizer");
    return YES;
}

然后当点击标签时,我得到以下日志:

shouldRecognizeSimultaneouslyWithGestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer
labelTap
viewTap

当点击视图时:

viewTap
于 2012-10-23T19:15:54.813 回答