SHPinchEvent

type:
extension
author:
Shilo White
description:
A simple pinch event.
lastupdate:
2011-06-04
compatible:
v1.X
tag:
pinch, event, pinchevent
homepage:
https://gist.github.com/1346529
download:
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

  1. Download the source files here: https://gist.github.com/gists/1346529/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 "SHPinchEvent.h"
  6. Save “Game.h”

To add directly into Sparrow source

  1. Download the source files here: https://gist.github.com/gists/1346529/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 “SHPinchEvent.h” and “SHPinchEvent.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 "SHPinchEvent.h"
  10. Save “Sparrow.h”
  11. 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

Changelog

  • 2011-06-04: First version

Todo

  • (Empty)
  users/shilo/extensions/shpinchevent.txt · Last modified: 2013/03/05 10:19 by 127.0.0.1
 
Powered by DokuWiki