如果我正在制作UITableViewCell可滑动(用于快速删除),我该如何制作它仍然可以点击?

时间:2013-04-27 19:56:14

标签: ios objective-c uitableview uigesturerecognizer

我已经成功地将我的UITableViewCell左右滑动以将单元格标记为已读或删除它(有点像Reeder应用程序如何执行)但现在它不允许我简单地点击它。我如何允许它被点击?

我使用this article作为概念的结构,因此可以获得更多信息。

我按如下方式实现了滑动:

- (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");
    }
}

当我点击它时,没有任何反应。

2 个答案:

答案 0 :(得分:1)

只需使用UIPanGestureRecognizer。

触摸开始的方法是如此古老。 使用平移手势,您不会丢失手机上的触摸事件。

答案 1 :(得分:0)

最容易解决的,实际上是建立自己的左右滑动手势。 构建它之后,只需将此手势添加到视图中,并添加轻击手势。

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

此处如何构建自定义手势识别器:

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