Skip to content

Instantly share code, notes, and snippets.

@krzysztofzablocki
Created August 2, 2012 19:58
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save krzysztofzablocki/3240133 to your computer and use it in GitHub Desktop.
Save krzysztofzablocki/3240133 to your computer and use it in GitHub Desktop.
Handling dial like rotation on CCNode by using UIPanGestureRecognizer
//! for gesture recognizer support in cocos2d use https://github.com/krzysztofzablocki/CCNode-SFGestureRecognizers
- (void)handlePanGesture:(UIPanGestureRecognizer*)gestureRecognizer
{
CGPoint location = [[CCDirector sharedDirector] convertToGL:[gestureRecognizer locationInView:gestureRecognizer.view]];
static CGPoint oldLocation;
//! this will make sure that oldLocation is initialized
if (gestureRecognizer.state != UIGestureRecognizerStateBegan) {
//! we need to calculate angle difference between previous position and current one in regards to dial center
CGPoint firstDir = ccpSub(oldLocation, dial.position);
CGPoint secondDir = ccpSub(location, dial.position);
prevAngle = -ccpToAngle(firstDir);
curAngle = -ccpToAngle(secondDir);
angleChange = CC_RADIANS_TO_DEGREES(curAngle - prevAngle);
dial.rotation += angleChange;
}
oldLocation = location;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment