====== SHSwipeEvent ====== ---- dataentry extension ---- type : extension author_mail : shilo86@gmail.com Shilo White description : A simple swipe event. lastupdate_dt : 2011-03-22 compatible : v1.X depends : tags : swipe, event, swipeevent homepage_url : https://gist.github.com/882536 download_url : https://gist.github.com/gists/882536/download sample_url : http://shilo.coarsemode.com/sparrow/extensions/shswipeevent/SnakeSample.zip min iOS version : 3.2 ---- ===== Description ===== 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. ===== Sample ===== [[http://shilo.coarsemode.com/sparrow/extensions/shswipeevent/SnakeSample.zip|{{http://shilo.coarsemode.com/sparrow/extensions/shswipeevent/SnakeScreenshot.png}}]] * Project: [[http://shilo.coarsemode.com/sparrow/extensions/shswipeevent/SnakeSample.zip|Snake Game]] * Screenshot: [[http://shilo.coarsemode.com/sparrow/extensions/shswipeevent/SnakeScreenshot.png|SnakeScreenshot.png]] * Recording: [[http://shilo.coarsemode.com/sparrow/extensions/shswipeevent/SnakeRecording.mov|SnakeRecording.mov]] ===== Example ===== @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 ===== Instructions ===== ==== To add into your Sparrow project ==== - Download the source files here: [[https://gist.github.com/gists/882536/download]] - Open your desired Sparrow project - Drag and drop the files into the "Groups & Files" pane - Open "Game.h" - Include the extension: #include "SHSwipeEvent.h" - Save "Game.h" ==== To add directly into Sparrow source ==== - Download the source files here: [[https://gist.github.com/gists/882536/download]] - Save the files into Sparrow's "/Classes/" directory * Example: "/sparrow/src/Classes/" - Navigate back one directory and open "Sparrow.xcodeproj" * Example: "/sparrow/src/Sparrow.xcodeproj" - Inside the "Groups & Files" pane, locate the "Events" group folder * Example: "Sparrow -> Classes -> Events" - Right click the "Display" group folder and click "Add -> Existing Files.." - Navigate into the "/Classes/" directory and select "SHSwipeEvent.h" and "SHSwipeEvent.m", then click "add" - On the next window, leave all options as default and click "add" - In the "Groups & Files" pane, Open "Sparrow.h" * Example "Sparrow -> Classes -> Sparrow.h" - At the bottom of the file, add: #import "SHSwipeEvent.h" - Save "Sparrow.h" - Close "Sparrow.xcodeproj" ===== Troubleshooting ===== ==== Why is it not recognizing swipes? ==== Make sure you add this line before listening for a SPSwipeEvent: [self.stage startSwipeRecognizer]; ==== Why is it not recognizing the correct swipe direction? ==== 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). ==== Why am I getting a runtime error/crash? ==== 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. ===== Source Code ===== %gist(882536)% ===== Changelog ===== * //2011-03-10:// First version * //2011-06-21:// Events now dispatch to hit display objects and bubble correctly. (Thank you odie) ===== Todo ===== * Add continuous swipe option