0

I've successfully made it that my UITableViewCell can be slid right and left to mark the cell as read, or to delete it (sort of like how the Reeder app does it) but now it doesn't allow me to simply tap it. How do I allow it to be tapped as well?

I used this article for the structure of the concept, so more information is available there.

I implemented the sliding as follows:

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

When I tap it though, nothing happens.

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 に答える