0

我已经成功地使我的 UITableViewCell 可以左右滑动以将单元格标记为已读或删除它(有点像 Reeder 应用程序的操作方式),但现在它不允许我简单地点击它。我如何让它也被窃听?

我将这篇文章用于概念的结构,因此可以在此处获得更多信息。

我实现了如下滑动:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    _firstTouchPoint = [[touches anyObject] locationInView:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint touchPoint = [[touches anyObject] locationInView: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;
        }
    }

    // Change our cellFront's origin to the xPos we defined
    CGRect frame = self.cellFront.frame;
    frame.origin = CGPointMake(xPos, 0);
    self.cellFront.frame = frame;

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self springBack];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self springBack];
}

- (void)springBack {
    CGFloat cellXPositionBeforeSpringBack = self.cellFront.frame.origin.x;

    CGRect frame = self.cellFront.frame;
    frame.origin = CGPointMake(0, 0);

    [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.cellFront.frame = frame;
    } completion:^(BOOL finished) {

    }];

    if (cellXPositionBeforeSpringBack >= 80 || cellXPositionBeforeSpringBack <= -80) {
        NSLog(@"YA");
    }
}

但是,当我点击它时,什么也没有发生。

4

2 回答 2

1

只需使用 UIPanGestureRecognizer。

touchesBegan 方法太老了。使用平移手势,您不会丢失单元格上的触摸事件。

于 2013-04-27T21:34:53.180 回答
0

最简单的解决方法,实际上是建立自己的左右滑动手势。构建完成后,只需将此手势添加到视图中,并添加点击手势。

如果您通过实现 'shouldRecognizeSimultaneouslyWithGestureRecognizer' 方法允许两个手势(点击手势和滑动手势)同时存在,iOS 将非常轻松地为您处理它。

这里如何构建自定义手势识别器:

http://blog.federicomestrone.com/2012/01/31/creating-custom-gesture-recognisers-for-ios/

于 2013-04-27T20:05:47.747 回答