在UITextField上滑动左手势 [英] Swipe left gestures over UITextField

查看:96
本文介绍了在UITextField上滑动左手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在tableviewcells中有UITextFields。当您滑动单元格而不是文本字段的一部分时,删除操作会按预期显示。如果您在文本字段上滑动,则会停止弹出删除。

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 stops the delete from popping up.

如何解决此问题,以便您可以滑动输入,单元格将触发删除动作?

How do I fix this so that you can swipe over the inputs and the cell will trigger the delete action?

推荐答案

我认为这里的问题是文本字段上的触摸会干扰您的滑动手势识别器(可能是附加的)到父视图)。我在UIScrollView中放置了一个类似的文本字段问题。

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.

我通过在UITextField上覆盖一个清晰的UIView解决了这个问题。然后,我为此清晰视图分配了一个UITapGestureRecognizer,以便在用户点击该字段时将文本字段设置为第一个响应者。否则,滑动被发送到父视图(滚动视图),该视图识别滑动而没有任何问题。它有点蹩脚,但它有效。

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

然后在控制器中:

- (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;
}

这篇关于在UITextField上滑动左手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆