Swipe left gestures over UITextField

后端 未结 3 1250
有刺的猬
有刺的猬 2021-01-12 15:11

I have UITextFields in tableviewcells. When you swipe over the cell not part of the textfield, the delete action comes up as expected. If you swipe over the textfield, it st

相关标签:
3条回答
  • 2021-01-12 15:29

    from UIButton & UITextField will block UITableViewCell being swipe to delete

    self.tableView.panGestureRecognizer.delaysTouchesBegan = YES;
    

    this will make the textfield not stop left swipe guestures

    0 讨论(0)
  • 2021-01-12 15:31

    Sounds like you need to set the cancelsTouchesInView property

    yourGestureRecognizer.cancelsTouchesInView = NO;
    
    0 讨论(0)
  • 2021-01-12 15:37

    I think the issue here is that the touch on the text field is interfering with your swipe gesture recognizer (presumably attached to the parent view). I had a similar problem with a text field that was placed in a UIScrollView.

    I got around this problem by overlaying a clear UIView on top of my UITextField. I then assigned a UITapGestureRecognizer to this clear view to set the text field as the first responder when the user taps on the field. Otherwise, the swiped is sent to the parent view (a scroll view) which recognizes the swipe without any issues. It's kind of lame but it works.

    This scenario is a bit different from yours, but I think it's the same problem. Here is what my code looks like, hopefully it 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;
    }
    
    0 讨论(0)
提交回复
热议问题