ios - 为什么我的 UIPanGestureRecognizer 没有被检测到并触发该方法?

标签 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 文件中,它会在实现时收到通知。但它从来没有像我说的那样调用它。

为什么?

最佳答案

这是你做的吗?

[self.contentView addGestureRecognizer:panGestureRecognizer];

关于ios - 为什么我的 UIPanGestureRecognizer 没有被检测到并触发该方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16258165/

相关文章:

ios - NSNotification 监听器 iOS

ios - 如何打开分数以使用 SpriteKit 创建 SKEmitterNode?

ios - 仅当当前值大于存储值时才覆盖 Firebase DB 值

ios - 在哪里存储购买的内容和导出的数据

ios - Xcode 6 和 iOS 8 更新后 UITableView 旋转表现奇怪

iphone - 为什么每次在 iOS 中滚动时 UITable 行都未选中

ios - 内容 View 不显示在 UITableViewCell 上

按钮/标签等的 iOS 命名约定

ios - 获取适当的 UITableViewCell 分隔 : GUI

ios - 如何让 UIKit 为我提供自定义转换的转换协调器?