I think the problem here is that touching the text field is interfering with your gesture recognizer (supposedly attached to the parent view). I had a similar problem with a text box placed in a UIScrollView.
I ran into this problem by laying a clean UIView on top of my UITextField. Then I assigned UITapGestureRecognizer to this clear view to set the text field as the first responder when the user clicks on the field. Otherwise, swiped is sent to the parent view (scroll view), which recognizes swipe without any problems. It is a little lame, but it works.
This scenario is a little different from yours, but I think the same problem. Here is what my code looks like, hope this helps:
// UIView subclass header @interface LSAddPageView : UIView @property (weak, nonatomic) IBOutlet UITextField *textField; // Connected to the UITextField in question @property (strong, nonatomic) UIView *textFieldMask; @property (assign, nonatomic) BOOL textFieldMaskEnabled; @end // UIView subclass implementation @implementation LSAddPageView - (void)awakeFromNib { [super awakeFromNib]; _textFieldMask = [UIView new]; _textFieldMask.backgroundColor = [UIColor clearColor]; [self insertSubview:_textFieldMask aboveSubview:self.textField]; } - (void)layoutSubviews { [super layoutSubviews]; _textFieldMask.frame = self.textField.frame; } - (BOOL)textFieldMaskEnabled { return _textFieldMask.hidden == NO; } - (void)setTextFieldMaskEnabled:(BOOL)textFieldMaskEnabled { _textFieldMask.hidden = !textFieldMaskEnabled; } @end
And then in the controller:
- (void)viewDidLoad { [super viewDidLoad]; _addPageView = (LSAddPageView*)self.view; _maskGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapMask:)]; _maskGestureRecognizer.numberOfTapsRequired = 1; _maskGestureRecognizer.numberOfTouchesRequired = 1; [_addPageView.textFieldMask addGestureRecognizer:_maskGestureRecognizer]; self.textField.delegate = self; // Set delegate to be notified when text field resigns first responder } - (void)didTapMask:(UIGestureRecognizer*)recognizer { _addPageView.textFieldMaskEnabled = NO; [self.textField becomeFirstResponder]; } - (BOOL)textFieldShouldEndEditing:(UITextField *)textField { _addPageView.textFieldMaskEnabled = YES; return YES; }
source share