====== SHPinchEvent ====== ---- dataentry extension ---- type : extension author_mail : shilo86@gmail.com Shilo White description : A simple pinch event. lastupdate_dt : 2011-06-04 compatible : v1.X depends : tags : pinch, event, pinchevent homepage_url : https://gist.github.com/1346529 download_url : https://gist.github.com/gists/1346529/download min iOS version : 3.2 ---- ===== Description ===== SHPinchEvent class will allow you to easily listen to pinch events inside Sparrow. The event will hold the local touch location, scale, velocity, and number of touches. It also contains a method to get the location based on spaces that inherit SPDisplayObject. Note: SHPinchEvent will only bubble to display objects with bounds containing the touch location. ===== Sample ===== (Coming later) ===== Example ===== @implementation Game - (id)initWithWidth:(float)width height:(float)height { if (self = [super initWithWidth:width height:height]) { //start pinch recognizer [self.stage startPinchRecognizer]; SPQuad *quad = [SPQuad quadWithWidth:50 height:50]; //add a swipe event listener [quad addEventListener:@selector(onPinch:) atObject:self forType:SH_EVENT_TYPE_PINCH]; [self.stage addChild:quad]; } return self; } - (void)onPinch:(SHPinchEvent *)event { SPPoint *localLocation = event.location; SPPoint *globalLocation = [event locationInSpace:self.stage]; float scale = event.scale; float velocity = event.velocity; uint numberOfTouches = event.numberOfTouches; } - (void)dealloc { //stop and remove pinch recognizer [self.stage stopPinchRecognizer]; [self removeChild:mContainer]; [super dealloc]; } @end ===== Instructions ===== ==== To add into your Sparrow project ==== - Download the source files here: [[https://gist.github.com/gists/1346529/download]] - Open your desired Sparrow project - Drag and drop the files into the "Groups & Files" pane - Open "Game.h" - Include the extension: #include "SHPinchEvent.h" - Save "Game.h" ==== To add directly into Sparrow source ==== - Download the source files here: [[https://gist.github.com/gists/1346529/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 "SHPinchEvent.h" and "SHPinchEvent.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 "SHPinchEvent.h" - Save "Sparrow.h" - Close "Sparrow.xcodeproj" ===== Troubleshooting ===== ==== Why is it not recognizing Pinches? ==== Make sure you add this line before listening for a SHPinchEvent: [self.stage startPinchRecognizer]; ==== 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 "startPinchRecognizer:" method after SPStage is linked to SPView. ===== Source Code ===== %gist(d22c35483be9d7a2b734)% ===== Changelog ===== * //2011-06-04:// First version ===== Todo ===== * (Empty)