1
votes

I have subclassed CCSprite class and added a UIRotationGestureRecognizer to it. So, in my init method I have this

UIRotationGestureRecognizer *rot = [[[UIRotationGestureRecognizer alloc]
                         initWithTarget:self action:@selector(handleRotation:)] autorelease];
[rot setDelegate:self];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:rot];

and then I have the method

- (void)handleRotation:(UIRotationGestureRecognizer *)recognizer {

    float rotation = [recognizer rotation];
    self.rotation += rotation;
}

it works perfectly but it has a huge lag between the actual gesture and the rotation itself. I would say almost 0.5 seconds between the gesture and the sprite response.

How do I solve that? thanks.


NOTE: After the first comment, I have added two more recognizers to the sprite: UIPinchGestureRecognizer and UIPanGestureRecognizer. I have also added the delegate method shouldRecognizeSimultaneouslyWithGestureRecognizer and set that to YES.

After doing this and checking, pinch and pan gestures are fast as hell. On the other hand, rotation continues slow. There was not reduction on the rotation speed by adding these two other gestures recognizer. The other two respond fluid and fast, UIRotationGestureRecognizer is slow.

1
Have you set any other gesture recognizer on the view?sergio
no. what you mean by view? the sprite? This is a question I have. When the gestureRecognizer is added using [[[CCDirector sharedDirector] openGLView], this openGLView sounds to me as the master openGLView holding all graphics. Shouldn't this be set to just the sprite? If so, how do I do that?Duck
the view I meant is rightly openGLView. It is correct to attach the gesture recognizer to that view (as you say, the master view), since it is the only view you have. I also do, and it does not add any delay. If you wereusing more gest. recognizers, then there could be some interacion, I thoughtsergio
I have edited the question to give more informations. I have added two more recognizers to the same sprite: pinch and pan. Pinch and Pan respond fast and fluid, but not UIRotateGestureRecognizer.... :(Duck

1 Answers

4
votes

Gesture rotation is in radians while Cocos2D rotation is in degrees. So you need to convert this as follows:

- (void)handleRotation:(UIRotationGestureRecognizer *)recognizer 
{
    float rotation = CC_RADIANS_TO_DEGREES([recognizer rotation]);
    self.rotation += rotation;
}

You can also save yourself these troubles and use Kobold2D, which not only adds an easy to use interface for gestures (and other input types) to Cocos2D but also converts the values accordingly to Cocos2D view coordinates and degrees. You'll never have to think about converting values again.