在UITextField上滑动左手势

时间:2013-09-25 22:34:56

标签: ios objective-c uitableview tableview

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

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

3 个答案:

答案 0 :(得分:2)

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

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

这种情况与您的情况略有不同,但我认为这是同样的问题。这是我的代码看起来像,希望它有所帮助:

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

答案 1 :(得分:1)

听起来你需要设置cancelsTouchesInView属性

yourGestureRecognizer.cancelsTouchesInView = NO;

答案 2 :(得分:1)

来自UIButton & UITextField will block UITableViewCell being swipe to delete

self.tableView.panGestureRecognizer.delaysTouchesBegan = YES;

这将使文本字段不会停止向左滑动客户