SHSwipeEvent class will allow you to easily listen to swipe events inside Sparrow, it also contains extra methods to get the location and direction based on spaces that inherit SPDisplayObject.
@implementation Game - (id)initWithWidth:(float)width height:(float)height { if (self = [super initWithWidth:width height:height]) { //set a container sprite in landscape mContainer = [SPSprite sprite]; mContainer.rotation = SP_D2R(90); mContainer.x = 320; [self addChild:mContainer]; //start swipe recognizer [self.stage startSwipeRecognizer]; //add a swipe event listener [self addEventListener:@selector(onSwipe:) atObject:self forType:SH_EVENT_TYPE_SWIPE]; } return self; } - (void)onSwipe:(SHSwipeEvent *)event { //get the global coordinates from the start of the swipe SPPoint *globalLocation = event.location; //get the local coordinates based on mContainer SPPoint *localLocation = [event locationInSpace:mContainer]; //get the global swipe direction SHSwipeDirection globalDirection = event.direction; //get the local swipe direction based on mContainer SHSwipeDirection localDirection = [event directionInSpace:mContainer]; //check the local swipe direction if (localDirection == SHSwipeDirectionUp) { NSLog(@"swiped up"); } else if (localDirection == SHSwipeDirectionDown) { NSLog(@"swiped down"); } else if (localDirection == SHSwipeDirectionLeft) { NSLog(@"swiped left"); } else if (localDirection == SHSwipeDirectionRight) { NSLog(@"swiped right"); } } - (void)dealloc { //stop and remove swipe recognizer [self.stage stopSwipeRecognizer]; [self removeChild:mContainer]; [super dealloc]; } @end
#include "SHSwipeEvent.h"
#import "SHSwipeEvent.h"
Make sure you add this line before listening for a SPSwipeEvent:
[self.stage startSwipeRecognizer];
if you are using “directionInSpace:” method with a SPDisplayObject that has a rotation that is not in increments of 90 degrees, you won't always get an accurate direction. This is because UISwipeGestureRecognizer does not hold the exactly angle/degree of the direction. Again, this is only if you use degrees that are not: (-180, -90, 0, 90, 180, 270, ect).
If you are encountering this runtime error:
*** Terminating app due to uncaught exception 'NativeViewDoesNotExist', reason: 'nativeView not linked to stage yet.'
Inside your ApplicationDelegate, I suggest you link your Game class to sparrowView (SPView) BEFORE initializing your Game class, like so:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { SP_CREATE_POOL(pool); [SPStage setSupportHighResolutions:YES]; [SPAudioEngine start]; Game *game = [Game alloc]; sparrowView.stage = game; game = [game init]; [game release]; [window makeKeyAndVisible]; [sparrowView start]; SP_RELEASE_POOL(pool); return YES; }
Warning: Although this method works fine in this situation, it might not be safe to get an instance of other Apple/Sparrow classes before initialization. The class might release and initialize a new instance of itself in the “init” method, which would make the old instance invalid.
You could also call “startSwipeRecognizer:” method after the initialization of your Game class to ensure that SPStage is linked to SPView.