Friday, September 27, 2013

Gesture Control - Swiping!

This is pretty cool. Lots of pre-built goodness in IOS6 that makes swipe gesture recognition a snap. This is essential for the snake game control system: driving the snake will be done by swiping.

Here's all you have to do to gather swipe input from your iphone screen:

UISwipeGestureRecognizer * leftSwipe = [[ UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
[view addGestureRecognizer:leftSwipe];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;

Set up several of these in your view controller initialization method, then implement the handler:

- (void) didSwipe: (UISwipeGestureRecognizer*) sender {
    UISwipeGestureRecognizerDirection direction = sender.direction;  
    switch( direction ) {
        case UISwipeGestureRecognizerDirectionLeft:            NSLog(@"left");
            break;
        case UISwipeGestureRecognizerDirectionRight:            NSLog(@"right");
            break;
    }
}

This is excerpted from this excellent youtube tutorial:

http://www.youtube.com/watch?v=5Ou8Oqhngm8

No comments:

Post a Comment