我在 tableviewcells 中有 UITextFields。当您在不属于文本字段的单元格上滑动时,删除操作会按预期出现。如果您在文本字段上滑动,它会停止弹出删除。
我该如何解决这个问题,以便您可以在输入上滑动,单元格将触发删除操作?
最佳答案
我认为这里的问题是文本字段上的触摸干扰了您的滑动手势识别器(可能附加到父 View )。对于放置在 UIScrollView 中的文本字段,我遇到了类似的问题。
我通过在我的 UITextField 上覆盖一个清晰的 UIView 来解决这个问题。然后,我将 UITapGestureRecognizer 分配给这个清晰的 View ,以在用户点击该字段时将文本字段设置为第一响应者。否则,滑动会被发送到父 View ( ScrollView ),它可以毫无问题地识别滑动。这有点蹩脚,但它有效。
这个场景和你的有点不一样,但是我觉得是同一个问题。这是我的代码,希望对您有所帮助:
// 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
然后在 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;
}
关于ios - 在 UITextField 上向左滑动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19016324/