2013-03-14 94 views
1

我已經在我的項目中實現了此自定義控件:Github page此控件添加了一個像滑動一樣的郵箱以刪除/完成我想要使用的功能。UITextfield干擾滑動手勢

我在使用這個時遇到的唯一問題是如果我通過故事板向單元格添加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]; 
    } 
} 
+0

要麼發佈您遇到的問題代碼,要麼回到正在使用該控件的人身上並向他尋求幫助。如果您在使用代碼時遇到困難,並且看到它我們可以爲您提供幫助,我們隨時爲您提供幫助。 – 2013-03-14 01:42:01

+0

對不起。我添加了代碼。 – cherbear 2013-03-14 02:20:41

+0

您發佈的代碼非常好。它應該工作。文本字段的代碼在哪裏?您正在通過故事板添加它,但爲了連接它並觸發手勢事件,它的代碼在哪裏? – 2013-03-14 02:31:44

回答

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是干擾因爲它不是細胞的子視圖。