Using UIGestureRecognizer with Cocos2D

Unfortunately Cocos2D does not support detecting standard gestures like pinch-to-zoom or two-finger-rotation by default, so one needed to develop own gesture-recognizers based on the coordinates you get by using CCStandardTouchDelegate. The good news is, that developer xemus has created a patch that adds support of Apple's UIGestureRecognizer to Cocos2D (as found in this forum discussion). Here is a short introduction on how to use this patch with Cocos2D 0.99.5-rc1.

cd <path-to-project>/libs/cocos2d 
patch -p1 -i cocos2d.patch
  • You might get an error, saying Hunk #3 FAILED at 269. The solution is to go to line 276 of the file CCNode.m and add the following lines:
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED 
        // lazy allocation 
        gestureRecognizers_ = nil; 
        isTouchEnabled_ = NO; 
#endif
  • Don't forget to add the files CCGestureRecognizer.h/.m to your XCode-Project, so that they will be compiled

That's all you need to get it running. Now you can use a new class named CCGestureRecognizer in your App like that:

// create a CCGestureRecognizer for rotation-gesture
CCGestureRecognizer* recognizer;
recognizer = [CCGestureRecognizer CCRecognizerWithRecognizerTargetAction:[[[UIRotationGestureRecognizer alloc]init] autorelease]    // use rotation recognizer
    target:self  // callback object -> this object gets called when there is rotation gesture
    action:@selector(rotateGesture:node:)];   // callback method -> rotateGesture:node: gets called when there is rotation gesture
[self addGestureRecognizer:recognizer]; // register the gesture recognizer

// create a CCGestureRecognizer for pinch-gesture
recognizer = [CCGestureRecognizer CCRecognizerWithRecognizerTargetAction:[[[UIPinchGestureRecognizer alloc]init] autorelease]    // use rotation recognizer
    target:self // callback object
    action:@selector(pinchGesture:node:)];  // callback method
[self addGestureRecognizer:recognizer]; // register the gesture recognizer

Implement your callback-methods that will receive the gesture-events. This sample codes will change the rotation and scale of a sprite.

// This method is called upon recognizing a gesture by the UIRotationGestureRecognizer
- (void)rotateGesture:(UIRotationGestureRecognizer *)recognizer node:(CCNode *)node {
    // get the rotation from the recognizer, convert it into degrees and set it to the sprite's rotation
    sprite.rotation = CC_RADIANS_TO_DEGREES([recognizer rotation]);
}

// This method is called upon recognizing a gesture by the UIPinchGestureRecognizer
- (void)pinchGesture:(UIPinchGestureRecognizer *)recognizer node:(CCNode *)node {
    // get the scale from the recognizer
    sprite.scale = [recognizer scale];
}

You can download a full documented sample-project and have a look in the TouchLayer.m file: Cocos2DTouch.zip

Comments

  1. Von kiran, am 21.11.2011 um 11:06
    its really great application for the gestures and cocos2DTouch.

    Can we restrict the moving zooming level's in a specified view frame.

    i need to do this zooming and moving inside a frame where its need to to cross and when it touchs the origin.x and origin.y its need to do stop gestures. can you suggest me how to implement that.

Schreiben Sie einen Kommentar

Alle Kommentare werden überprüft, bevor sie freigeschalten werden.


Name (Pflichtfeld):

E-mail (Pflichtfeld, wird nicht veröffentlicht):

Website:

Bitte die Zahl eingeben:


< Zurück

Seite zuletzt bearbeitet am 10.01.2011 18:30 Uhr