UITextfield会干扰滑动手势

时间:2013-03-14 01:03:58

标签: ios cocoa-touch uitableview uigesturerecognizer

我在我的项目中实现了这个自定义控件: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];
    }
}

1 个答案:

答案 0 :(得分:0)

弄清楚如何解决这个问题。我在项目中添加了一个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正在干扰,因为它不是该单元的子视图。