缩放SKNode不一致

时间:2013-10-19 02:19:55

标签: ios sprite-kit

我已经创建了自己的放大或缩小特定SKNode的解决方案,而没有缩放整个场景,它似乎主要用于我期望它如何工作,我希望得到两个明显的例外在这里输入。首先是代码(这个控制语句在touchesMoved方法中):

 if (touches.count == 2) {
        // this means there are two fingers on the screen
        NSArray *fingers = [touches allObjects];
        CGPoint fingOneCurr = [fingers[0] locationInNode:self];
        CGPoint fingOnePrev = [fingers[0] previousLocationInNode:self];
        CGPoint fingTwoCurr = [fingers[1] locationInNode:self];
        CGPoint fingTwoPrev = [fingers[1] previousLocationInNode:self];

        BOOL yPinch = fingOneCurr.y > fingOnePrev.y && fingTwoCurr.y < fingTwoPrev.y;
        BOOL yUnpinch = fingOneCurr.y < fingOnePrev.y && fingTwoCurr.y > fingTwoPrev.y;

        BOOL xPinch = fingOneCurr.x > fingOnePrev.x && fingTwoCurr.x < fingTwoPrev.x;
        BOOL xUnpinch = fingOneCurr.x < fingOnePrev.x && fingTwoCurr.x > fingTwoPrev.x;

        if (xUnpinch | yUnpinch) {
            if (YES) NSLog(@"This means an unpinch is happening");
            mapScale = mapScale +.02;
            [map setScale:mapScale];
        }

        if (xPinch | yPinch) {
            if (YES) NSLog(@"This means a pinch is happening");
            mapScale = mapScale - .02;
            [map setScale:mapScale];
        }
    }

现在出现问题:

  1. 有时候捏合和捏合并不总是正确的,当发生这种情况时,我无法完全按下手指,捏合将表现为一种旋转,反之亦然。

  2. 当收缩和取消固定正确缩放SKNode时,它很少像我想的那样平滑。它有点急躁,我觉得很讨厌。

  3. 有人可以建议改进这种方法吗?谢谢!

1 个答案:

答案 0 :(得分:8)

这将解决您的问题,感谢Steffen的提示。

- (void)didMoveToView:(SKView *)view
{
    UIPinchGestureRecognizer *precog = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
    [self.scene.view addGestureRecognizer:precog];
}

- (void)handlePinch:(UIPinchGestureRecognizer *) recognizer
{
    //NSLog(@"Pinch %f", recognizer.scale);
    //[_bg setScale:recognizer.scale];
    [_bg runAction:[SKAction scaleBy:recognizer.scale duration:0]];
    recognizer.scale = 1;
}