在IOS中,UIGesture因未知原因而失败

时间:2011-01-23 04:06:42

标签: iphone ios uiresponder

我有一个UITextField,它是UIScrollView的子视图 我有一个单击手势(或者可能是较旧的触摸)作为UITextField的一部分,使其成为第一个响应者。我想在UITextField中添加双击手势。为了使两个Gesture相互排斥,我需要重载(尽管这不是一个正确的词,因为我没有使用相同的名称)单击手势。当我这样做时,只要它调用的函数不会使UITextField成为第一个响应者,它就可以正常工作。如果它确实使文本字段成为第一个响应者,那么它第二次失败就会被调用.... wtf。所以堆叠,一点帮助就会有很长的路要走。

这是我添加手势的地方

 UITapGestureRecognizer * singleTextTap = [[UITapGestureRecognizer alloc] 
             initWithTarget:self action:@selector(handleSingleTextTap:)];         
 singleTextTap.numberOfTapsRequired = 1;
 [aTextField addGestureRecognizer:singleTextTap];

这就是所谓的

//single tap text
- (void) handleSingleTextTap:(UITapGestureRecognizer *)gestureRecognizer{
    NSLog(@"handling single tap");
    NSLog(@"textField gestures %@", 
          [gestureRecognizer.view.gestureRecognizers descriptionWithLocale:nil]);
    [(UITextField *)gestureRecognizer.view becomeFirstResponder];
}

您可能会注意到NSLog将注册的gestuers注册到textField,这是为了检查我的singleTextTap手势的状态。这是第一次“结束”,下次所有代码都没有到达

2 个答案:

答案 0 :(得分:2)

您所看到的行为是由于您每次向becomeFirstResponder发送UITextField消息时都会将其设置为自己的手势识别器。如果您登录[textField gestureRecognizers],您会看到它设置了很多。这些是处理文本编辑功能,如长按光标移动等。通过添加这些来取消您已设置的那些。解决这个问题的最简单方法就是这样。

- (void) singleTap:(UITapGestureRecognizer*)singleTap {
    if (![self.textField isFirstResponder]) {
        [self.textField becomeFirstResponder];
        self.label.text = @"Single Tap -> Make First Responder";
        [self.label setNeedsDisplay];
    }
}
- (void) doubleTap:(UITapGestureRecognizer*)doubleTap {
    if (![self.textField isFirstResponder]) {
        [self.textField becomeFirstResponder];
        self.label.text = @"Double Tap -> Make First Responder";
        [self.label setNeedsDisplay];
    }
}

- (void) setupTextGestureRecognizers {
    for (UIGestureRecognizer *rec in self.textField.gestureRecognizers) {
        [self.textField removeGestureRecognizer:rec];
    }
    UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)] autorelease];
    UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)] autorelease];
    [singleTap setNumberOfTapsRequired:1];
    [doubleTap setNumberOfTapsRequired:2];
    [singleTap setCancelsTouchesInView:YES];
    [doubleTap setCancelsTouchesInView:YES];
    [singleTap requireGestureRecognizerToFail:doubleTap];
    [self.textField addGestureRecognizer:singleTap];
    [self.textField addGestureRecognizer:doubleTap];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTextGestureRecognizers];
    self.textField.delegate = self;
}


- (BOOL) textFieldShouldReturn:(UITextField *)tf {
    [tf resignFirstResponder];
    if (tf == self.textField) {
        [self setupTextGestureRecognizers];
    }
    return YES;
}

以下是example project演示正在使用的此代码。

答案 1 :(得分:1)

你在做什么似乎很奇怪,但无论如何。我会假设你知道你在做什么。

您可以尝试以下方法:

UITapGestureRecognizer *doubleTap = ...;
UITapGestureRecognizer *singleTap = ...;
[singleTap requireGestureRecognizerToFail:doubleTap];

这意味着单击识别器只会在双击时不会触发,有效地使它们互相排斥。