为什么我的UIPanGestureRecognizer未被检测到并触发该方法?

时间:2013-04-28 00:02:23

标签: ios objective-c uitableview uigesturerecognizer uipangesturerecognizer

我有一个UITableView,我想将UIPanGestureRecognizer附加到每个单元格,这些单元格是子类UITableViewCells - ArticleCells。在awakeFromNib方法中,我添加了平移手势识别器,但它永远不会触发。为什么呢?

- (void)awakeFromNib {
    [super awakeFromNib];

    self.cellBack = [[CellBack alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 80)];
    [self.contentView addSubview:self.cellBack];

    self.cellFront = [[CellFront alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 80)];
    [self.contentView addSubview:self.cellFront];

    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pannedCell:)];
    panGestureRecognizer.delegate = self;
}

应该解雇这个方法。但我在它上面放了一个断点,它永远不会被解雇。

- (void)pannedCell:(UIPanGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        _firstTouchPoint = [recognizer translationInView:self];
        NSLog(@"fired");
    }
    else if (recognizer.state == UIGestureRecognizerStateChanged) {
        NSLog(@"fired");
        CGPoint touchPoint = [recognizer translationInView:self];

        // Holds the value of how far away from the first touch the finger has moved
        CGFloat xPos;

        // If the first touch point is left of the current point, it means the user is moving their finger right and the cell must move right
        if (_firstTouchPoint.x < touchPoint.x) {
            xPos = touchPoint.x - _firstTouchPoint.x;

            if (xPos <= 0) {
                xPos = 0;
            }
        }
        else {
            xPos = -(_firstTouchPoint.x - touchPoint.x);

            if (xPos >= 0) {
                xPos = 0;
            }
        }

        if (xPos > 10 || xPos < -10) {
            // Change our cellFront's origin to the xPos we defined
            CGRect frame = self.cellFront.frame;
            frame.origin = CGPointMake(xPos, 0);
            self.cellFront.frame = frame;
        }
    }
    else if (recognizer.state == UIGestureRecognizerStateEnded) {
        [self springBack];
    }
    else if (recognizer.state == UIGestureRecognizerStateCancelled) {
        [self springBack];
    }
}

在我添加的.h文件中,它将被通知为实现它。但它从来没有像我说的那样称呼它。

为什么?

1 个答案:

答案 0 :(得分:2)

你做到了吗?

[self.contentView addGestureRecognizer:panGestureRecognizer];