【问题标题】:UITextfield interferes with swipe gesturesUITextfield 干扰滑动手势
【发布时间】:2013-03-14 01:03:58
【问题描述】:

我在我的项目中实现了这个自定义控件:Github page 这个控件添加了一个邮箱,比如滑动删除/完成功能,我想使用它。

我在使用它时遇到的唯一问题是,如果我通过情节提要向单元格添加 UITextField。当我添加一个单元格时,它会停止识别手势,只允许我与 UITextField 交互。

有没有人可以解决这个问题?

编辑:这里是 TableViewCell 的初始化方法。对不起。

- (void)initializer
{
    _mode = MCSwipeTableViewCellModeSwitch;

    _colorIndicatorView = [[UIView alloc] initWithFrame:self.bounds];
    [_colorIndicatorView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
    [_colorIndicatorView setBackgroundColor:[UIColor clearColor]];
    [self insertSubview:_colorIndicatorView belowSubview:self.contentView];

    _slidingImageView = [[UIImageView alloc] init];
    [_slidingImageView setContentMode:UIViewContentModeCenter];
    [_colorIndicatorView addSubview:_slidingImageView];

    _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGestureRecognizer:)];
    [self addGestureRecognizer:_panGestureRecognizer];
    [_panGestureRecognizer setDelegate:self];
}

以及处理手势的方法

- (void)handlePanGestureRecognizer:(UIPanGestureRecognizer *)gesture
{
    UIGestureRecognizerState state          = [gesture state];
    CGPoint translation                     = [gesture translationInView:self];
    CGPoint velocity                        = [gesture velocityInView:self];
    CGFloat percentage                      = [self percentageWithOffset:CGRectGetMinX(self.contentView.frame) relativeToWidth:CGRectGetWidth(self.bounds)];
    NSTimeInterval animationDuration        = [self animationDurationWithVelocity:velocity];
    _direction = [self directionWithPercentage:percentage];

    if (state == UIGestureRecognizerStateBegan)
    {
    }
    else if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
    {
        CGPoint center = {self.contentView.center.x + translation.x, self.contentView.center.y};
        [self.contentView setCenter:center];
        [self animateWithOffset:CGRectGetMinX(self.contentView.frame)];
        [gesture setTranslation:CGPointZero inView:self];
    }
    else if (state == UIGestureRecognizerStateEnded || state == UIGestureRecognizerStateCancelled)
    {
        _currentImageName = [self imageNameWithPercentage:percentage];
        _currentPercentage = percentage;
        MCSwipeTableViewCellState state = [self stateWithPercentage:percentage];

        if (_mode == MCSwipeTableViewCellModeExit && _direction != MCSwipeTableViewCellDirectionCenter && [self validateState:state])
            [self moveWithDuration:animationDuration andDirection:_direction];
        else
            [self bounceToOriginWithDirection:_direction];
    }
}

【问题讨论】:

  • 要么发布您遇到问题的代码,要么直接回到制作您正在使用的控件的人那里寻求帮助。如果您在使用代码时遇到问题,我们随时为您提供帮助,通过查看代码,我们可以为您提供帮助。
  • 您发布的代码非常好。它应该工作。文本字段的代码在哪里?您正在通过情节提要添加它,但是为了连接它并触发手势事件,它的代码在哪里?
  • 我在我的initWithStyle 方法中添加了这个[self.itemLabel addGestureRecognizer:_panGestureRecognizer];(itemLabel 是我的textField),但它似乎不起作用。我应该将此添加到自定义初始化程序中吗?
  • 我不能肯定,但你可以试试。无论如何,我将下载该控件并查看它。与此同时,其他人也许可以提供帮助。
  • 感谢您迄今为止的帮助,并进一步提供帮助。

标签: ios cocoa-touch uitableview uigesturerecognizer


【解决方案1】:

想出了如何解决这个问题。我在我的项目中添加了一个 UITextField 子类,然后将 UITextfield 标头导入到自定义 UITableViewCell 中。将<UITextFieldDelegate> 添加到@interface。将@property (nonatomic, strong, readonly) ItemLabel *label; // ItemLabel being the UITextField class 添加到UITableViewCell 类中,在您的实现中综合它。

然后在您的自定义初始化程序中添加此代码,如果您没有自定义初始化程序,则添加默认代码:

_label = [[TehdaLabel alloc] initWithFrame:CGRectNull];
_label.textColor = [UIColor blackColor];
_label.font = [UIFont boldSystemFontOfSize:14];
_label.backgroundColor = [UIColor clearColor];
_label.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_label.returnKeyType = UIReturnKeyDone;
_label.delegate = self;
[self addSubview:_label];

然后为 UITextField 添加您的委托方法并快乐。 似乎 UITextField 正在干扰,因为它不是单元格的子视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    相关资源
    最近更新 更多