1

I have been trying to implement a standard pinch/zoom on a CCLayer in cocos2d (using the Kobold2D gesture recognisers) but with only partial success.

Below is my code which does make pinch/zoom work, so long as the pinch point doesn't move. However if I zoom in over one point on the layer and then lift off and move my fingers to zoom in further over another point, there is an instantaneous jump of the layer. It jumps to where the layer would have been if I'd been zooming in over the second point from the start, instead of simply zooming smoothly from where it was.

Can you see what I'm doing wrong or have I missed an existing simple pinch/zoom algorithm that does this job for CCLayers?

NB: I've left the default (YES) value for ignoreAnchorInPosition. Also, at the start self.scalePrePinch = 1.0f

-(void) update:(ccTime)delta
{
    KKInput* input = [KKInput sharedInput];
    if (input.gesturePinchBegan) {
        CGSize scr = [[CCDirector sharedDirector] screenSize];
        CGPoint pinchLocation = [self convertToNodeSpace:input.gesturePinchLocation];
        CGPoint anchor = ccp(pinchLocation.x/scr.width, pinchLocation.y/scr.height);
        CGFloat newScale = input.gesturePinchScale * self.scalePrePinch;
        self.anchorPoint = ccp(self.anchorPoint.x + self.scale / newScale * (anchor.x - self.anchorPoint.x), 
                               self.anchorPoint.y + self.scale / newScale * (anchor.y - self.anchorPoint.y));
        self.scale = newScale;
    }
    else
        self.scalePrePinch = self.scale;
}
Paul Masri-Stone
  • 2,843
  • 3
  • 29
  • 51

0 Answers0