SHSwipeEvent

type:
extension
author:
Shilo White
description:
A simple swipe event.
lastupdate:
2011-03-22
compatible:
v1.X
tag:
swipe, event, swipeevent
homepage:
https://gist.github.com/882536
download:
https://gist.github.com/gists/882536/download
sample:
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

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

  1. Download the source files here: https://gist.github.com/gists/882536/download
  2. Open your desired Sparrow project
  3. Drag and drop the files into the “Groups & Files” pane
  4. Open “Game.h”
  5. Include the extension:
    #include "SHSwipeEvent.h"
  6. Save “Game.h”

To add directly into Sparrow source

  1. Download the source files here: https://gist.github.com/gists/882536/download
  2. Save the files into Sparrow's “/Classes/” directory
    • Example: “/sparrow/src/Classes/”
  3. Navigate back one directory and open “Sparrow.xcodeproj”
    • Example: “/sparrow/src/Sparrow.xcodeproj”
  4. Inside the “Groups & Files” pane, locate the “Events” group folder
    • Example: “Sparrow → Classes → Events”
  5. Right click the “Display” group folder and click “Add → Existing Files..”
  6. Navigate into the “/Classes/” directory and select “SHSwipeEvent.h” and “SHSwipeEvent.m”, then click “add”
  7. On the next window, leave all options as default and click “add”
  8. In the “Groups & Files” pane, Open “Sparrow.h”
    • Example “Sparrow → Classes → Sparrow.h”
  9. At the bottom of the file, add:
    #import "SHSwipeEvent.h"
  10. Save “Sparrow.h”
  11. 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

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
  users/shilo/extensions/shswipeevent.txt · Last modified: 2013/03/05 10:19 (external edit)
 
Powered by DokuWiki