Search code examples
iphoneiosobjective-cxcodeuiswipegesturerecognizer

SwipeGesture in Iphone Cocos2d


I am using UISwipeGestureRecognizer to get swipe gesture in iPhone. i want to get 2 location points on Began of swipe touch and on end of swipe touch. i have implemented swipe touch method as below

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {

    CGPoint touchLocation = [recognizer locationInView:recognizer.view];
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
    touchLocation = [self convertToNodeSpace:touchLocation];

    CCLOG(@"Hello %@ - %d",NSStringFromCGPoint(touchLocation),recognizer.state);

    if (recognizer.state == UIGestureRecognizerStateBegan) {

        CGPoint touchLocation = [recognizer locationInView:recognizer.view];
        touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
        touchLocation = [self convertToNodeSpace:touchLocation];

        CCLOG(@"UIGestureRecognizerStateBegan %@",NSStringFromCGPoint(touchLocation));

    } else if (recognizer.state == UIGestureRecognizerStateChanged) {    

        CCLOG(@"UIGestureRecognizerStateChanged");

    } else if (recognizer.state == UIGestureRecognizerStateEnded) {
        CGPoint touchLocationEnd = [recognizer locationInView:recognizer.view];
        touchLocationEnd = [[CCDirector sharedDirector] convertToGL:touchLocationEnd];
        touchLocationEnd = [self convertToNodeSpace:touchLocationEnd];
        CCLOG(@"UIGestureRecognizerStateEnded %@",NSStringFromCGPoint(touchLocationEnd));
        }        

    //}
}

My swipe touch is working. but it only shows UIGestureRecognizerStateEnded. even when i swipe on screen and my touch is not ended yet but the StateEnded is called. how can i call StateBegin and get location and then StateEnd. right now just StateEnded is working other two Begin and Changing are not working.


Solution

  • Update: I find the reason:

    A swipe is a discrete gesture, and thus the associated action message is sent only once per gesture.

    And this image: enter image description here

    So there are only three states for UISwipeGestureRecognizer:possible, recognized and ended. But I still don't know why possible is not called.


    I tried your code and get the same result. I also don't know why this happen. If you can't find solution for this problem I suggest you use the for touch methods to analysis the touch yourself. This can surely handle the event but a little complicated than use gesture recognizer.

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;