通过 UITapGestureRecognizer 的 -locationInView 初始化触摸

Init touch by UITapGestureRecognizer's -locationInView

我在 tableView 上使用 UITapGestureRecognizer 在 -textFieldDidBeginEditing 之后进行 -endEditing。首先我想制作 -endEditing 然后在 tableView 的元素上执行此触摸。我怎样才能正确地做到这一点?

#pragma mark - Text Field Delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(finishEditing:)];
    [self.tableView addGestureRecognizer:tapRecognizer];
}

- (void)finishEditing:(UITapGestureRecognizer *)tapRecognizer {
    [self.view endEditing:YES];

}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [self saveName];
}

cancelsTouchesInView 设置为 false 会将触摸传递到视图。

When this property is true (the default) and the receiver recognizes its gesture, the touches of that gesture that are pending are not delivered to the view and previously delivered touches are cancelled through a touchesCancelled:withEvent: message sent to the view. If a gesture recognizer doesn’t recognize its gesture or if the value of this property is false, the view receives all touches in the multi-touch sequence.

见参考资料here