缩放SKNode不一致

zee*_*ple 6 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];
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在的问题:

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

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

任何人都可以建议改进这种方法吗?谢谢!

Dog*_*fee 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;
}
Run Code Online (Sandbox Code Playgroud)