Skip to content

Instantly share code, notes, and snippets.

@fictorial
Created May 9, 2012 20:21
Show Gist options
  • Save fictorial/2648522 to your computer and use it in GitHub Desktop.
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
- (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]];
}
}
@fictorial
Copy link
Author

Thanks to Alexey Lang for helping me get over my brain fart :-)

@dmquan
Copy link

dmquan commented Oct 2, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment