Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when the finger lifts after a UISwipeGesture[Recognizer]

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?

like image 846
Arseniy Banayev Avatar asked Aug 06 '10 19:08

Arseniy Banayev


2 Answers

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.
    }
}
like image 172
dalton_c Avatar answered Oct 19 '22 10:10

dalton_c


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.

like image 1
Arseniy Banayev Avatar answered Oct 19 '22 11:10

Arseniy Banayev