Created
May 9, 2012 20:21
-
-
Save fictorial/2648522 to your computer and use it in GitHub Desktop.
CCLayerPanZoom double-tap to zoom in/out with zoom-towards-point like in the Maps app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void) layerPanZoom: (CCLayerPanZoom *) sender | |
clickedAtPoint: (CGPoint) point | |
tapCount: (NSUInteger) tapCount | |
{ | |
NSLog(@"CCLayerPanZoomTestLayer#layerPanZoom: %@ clickedAtPoint: { %f, %f }", sender, point.x, point.y); | |
if (tapCount == 2) { | |
// Toggle zooming all the way in and all the way out. | |
float midScale = (sender.minScale + sender.maxScale) / 2.0; | |
float newScale = (sender.scale <= midScale) ? sender.maxScale : sender.minScale; | |
// Zoom towards the tap point. Note that scaling is performed "around" the anchor point. | |
// Thus, we need to account for how far the layer should be translated after its scaling | |
// change is performed. | |
CGPoint anchorPointNodeSpace = ccp(sender.anchorPoint.x * sender.contentSize.width, | |
sender.anchorPoint.y * sender.contentSize.height); | |
CGFloat deltaScale = newScale - sender.scale; | |
CGPoint deltaPos = ccpMult(ccpSub(point, anchorPointNodeSpace), deltaScale); | |
// Scale and translate in parallel. We negate the direction since to zoom to the point | |
// we want to move the layer in the opposite direction. | |
[sender runAction:[CCSpawn actions: | |
[CCScaleTo actionWithDuration:0.4 scale:newScale], | |
[CCMoveBy actionWithDuration:0.4 position:ccpNeg(deltaPos)], | |
nil]]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi fictorial,
I'm a newbie used cocos2dx on Android. I don't know how to use your code,is this Object C code ? Please help me ! Thanks.