Question

I've set up a UISwipeGestureRecognizer:

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:delegate action:@selector(handleSwipeGesture:)];
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[self addGestureRecognizer:swipe];
[swipe release];

A swipe makes the player move in the direction of the swipe. I need the player to keep moving, though, until the finger that made the swipe is lifted off the screen. I've tried using the touchesEnded: method but it requires that a non-swiping touch be made first. How can I get the touch that made the swipe gesture? How can I detect when that touch is lifted off the screen?

Was it helpful?

Solution 2

After looking through Apple's documentation, I found this property of UIGestureRecognizer:

@property(nonatomic) BOOL cancelsTouchesInView

Setting it to NO lets the receiver's view handle all touches that are part of the multi-touch sequence the gesture recognizer receives.

OTHER TIPS

I know you're already satisfied with an answer to this question, but I thought I might recommend using a UIPanGestureRecognizer instead of the swipe gesture.

With a pan gesture recognizer, the message is sent to the selector repeatedly until the user stops dragging their finger, at which point the selector is called one more time, passing a gesture.state of UIGestureRecognizerStateEnded. Example:

- (void)panGesture:(UIPanGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateEnded) {
        CGPoint translation = [gesture translationInView:self.view];
        //This contains the total translation of the touch from when it 
        //first recognized the gesture until now.
        //
        //e.g (5, -100) would mean the touch dragged to the right 5 points, 
        //and up 100 points.
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top