如何在cocos2d中更改缩放中心?

时间:2013-01-10 08:57:22

标签: ios cocos2d-iphone uigesturerecognizer

我想缩放到触摸位置,但此代码始终缩放到屏幕中心。

-(id)init{
 UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action: @selector(handleDoubleTapFrom:)];
    doubleTap.numberOfTapsRequired = 2;
    doubleTap.numberOfTouchesRequired = 1;
    [[[CCDirector sharedDirector] view] addGestureRecognizer:doubleTap];
}

- (void)handleDoubleTapFrom:(UITapGestureRecognizer *)recognizer {

    //CGPoint touchLocation = [recognizer locationInView:[[CCDirector sharedDirector]view] ];
    if(!isGameFinished){
        if(zoomPerformed == NO ) {
            id zoomIn = [CCScaleTo actionWithDuration:1.0f scale:2];
            id sequence = [CCSequence actions:zoomIn, nil];
            [self runAction:sequence];
            zoomPerformed = YES;
        }else{
            id zoomOut = [CCScaleTo actionWithDuration:1.0f scale:1.0f];
            id sequence = [CCSequence actions:zoomOut, nil];
            [self runAction:sequence];
            zoomPerformed = NO;
        }
    }
}

如何更改缩放原点?我搜索但没有一种方法成功。

1 个答案:

答案 0 :(得分:0)

您需要移动正在缩放(缩放)的图层,以使与触摸位置对应的点位于屏幕的中心。

CGSize winSize = [[CCDirector sharedDirector] winSize];
CGPoint center = ccpMult(ccpFromSize(winSize), 0.5);
[self runAction:[CCMoveTo actionWithDuration:1.0f position:ccpSub(center, touchLocation)]];

缩小时,您还需要重置图层的位置。