Search code examples
cocos2d-iphonebox2dbox2d-iphone

Proper Swipe gesture recognizer iOS


I haven't been able to find a tutorial on how to properly setup a gesture recognizer for iOS. I need to detect swipe up & down, and the callbacks for them.

Any help, appreciated. Thanks.


Solution

  • You need two recognizers, one for swiping up, and the other for swiping down:

    UISwipeGestureRecognizer* swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeUpFrom:)];
    swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
    

    and for the handler:

    - (void)handleSwipeUpFrom:(UIGestureRecognizer*)recognizer {
    
    }
    

    Finally, you add it to your view:

    [view addGestureRecognizer:swipeUpGestureRecognizer];
    

    The same for the other direction (just change all the Ups to Downs).