Skip to content

CCGestureRecognizer

spalx edited this page Apr 29, 2013 · 13 revisions

CCGestureRecognizer is an abstract base class for gesture recognizer classes.

The subclasses of CCGestureRecognizer are the following:

Recognizing gestures on specific nodes

The frame of the CCNode object (CCSprite, CCLayer, etc.) on which a gesture recognizer is added that's the space where the gesture recognizer is available. So if you add a gesture recognizer on a "full-screen" CCLayer you will be able to recognize the gesture everywhere on the screen. This is a great feature when you need to recognize gestures on specific parts of the screen.

Setting the target

When a gesture is recognized, your specified method gets called. You can set the method which you want to be called by the following way:

myGesture->setTarget(this, callfuncO_selector(HelloWorld::didRecognizeGesture));

The targeted method must have 1 parameter of CCObject* type and return void:

void didRecognizeGesture(CCObject * obj);

The received parameter type it's different in every case. For example, for the swipe gesture it's a CCSwipe object, for tap gesture it's a CCTap, and so on. So don't forget to cast correctly before starting to use it:

CCSwipe * swipe = (CCSwipe*)obj;

Canceling touches on other nodes

By default, when a gesture is recognized the touch propagates to other nodes below him, but you can stop the touch propagation setting the following property to true:

myGesture->setCancelsTouchesInView(true);