Ios Sprite套件夹点到缩放问题UIPinchGestureRecognitor

Ios Sprite套件夹点到缩放问题UIPinchGestureRecognitor,ios,objective-c,zooming,sprite-kit,pinch,Ios,Objective C,Zooming,Sprite Kit,Pinch,我已经在这段代码上工作了很长一段时间了,但感觉就像是前进了一步,后退了两步。我希望有人能帮助我 我正在使用Sprite工具包,所以我有一个场景文件来管理渲染、UI和触摸控件。我有一个SKNode,它可以像照相机一样工作: _world = [[SKNode alloc] init]; [_world setName:@"world"]; [self addChild:_world]; 我正在使用UIgestureRecognitor,因此我添加了我需要的,如: _panRecognizer =

我已经在这段代码上工作了很长一段时间了,但感觉就像是前进了一步,后退了两步。我希望有人能帮助我

我正在使用Sprite工具包,所以我有一个场景文件来管理渲染、UI和触摸控件。我有一个SKNode,它可以像照相机一样工作:

_world = [[SKNode alloc] init];
[_world setName:@"world"];
[self addChild:_world];
我正在使用UIgestureRecognitor,因此我添加了我需要的,如:

_panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanFrom:)];
[[self view] addGestureRecognizer:_panRecognizer];
_pinchRecognizer = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
[[self view] addGestureRecognizer:_pinchRecognizer];
平移效果不错,但不是很好。这才是真正的问题。收缩的想法是抓住屏幕中心的一个点,将该点转换为世界节点,然后在放大时移动到该点。以下是挤压的方法:

-(void) handlePinch:(UIPinchGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
    _tempScale = [sender scale];
}
if (sender.state == UIGestureRecognizerStateChanged) {
    if([sender scale] > _tempScale) {
        if (_world.xScale < 6) {
            //_world.xScale += 0.05;
            //_world.yScale += 0.05;
            //[_world setScale:[sender scale]];
            [_world setScale:_world.xScale += 0.05];

            CGPoint screenCenter = CGPointMake(_initialScreenSize.width/2, _initialScreenSize.height/2);
            CGPoint newWorldPoint = [self convertTouchPointToWorld:screenCenter];

            //crazy method why does this work
            CGPoint alteredWorldCenter = CGPointMake(((newWorldPoint.x*_world.xScale)*-1), (newWorldPoint.y*_world.yScale)*-1);

            //why does the duration have to be exactly 0.3 to work
            SKAction *moveToCenter = [SKAction moveTo:alteredWorldCenter duration:0.3];
            [_world runAction:moveToCenter];
        }
    } else if ([sender scale] < _tempScale) {
        if (_world.xScale > 0.5 && _world.xScale > 0.3){
            //_world.xScale -= 0.05;
            //_world.yScale -= 0.05;
            //[_world setScale:[sender scale]];
             [_world setScale:_world.xScale -= 0.05];

            CGPoint screenCenter = CGPointMake(_initialScreenSize.width/2, _initialScreenSize.height/2);
            CGPoint newWorldPoint = [self convertTouchPointToWorld:screenCenter];

            //crazy method why does this work
            CGPoint alteredWorldCenter = CGPointMake(((newWorldPoint.x*_world.xScale - _initialScreenSize.width)*-1), (newWorldPoint.y*_world.yScale - _initialScreenSize.height)*-1);

            SKAction *moveToCenter = [SKAction moveTo:alteredWorldCenter duration:0.3];
            [_world runAction:moveToCenter];
        }
    }
}
if (sender.state == UIGestureRecognizerStateEnded) {
    [_world removeAllActions];
}
}
-(void)handlePinch:(UIPinchGestureRecognitor*)发送方{
if(sender.state==UIgestureRecognitizerStateStart){
_tempScale=[sender scale];
}
if(sender.state==UIGestureRecognitzerStateChanged){
如果([发送方刻度]>_tempScale){
如果(_world.xScale<6){
//_world.xScale+=0.05;
//_world.yScale+=0.05;
//[_世界设置刻度:[发送方刻度]];
[_worldsetscale:_world.xScale+=0.05];
CGPoint screenCenter=CGPointMake(_initialScreenSize.width/2,_initialScreenSize.height/2);
CGPoint newWorldPoint=[self-convertTouchPointToWorld:screenCenter];
//疯狂的方法为什么这样做
CGPoint alteredWorldCenter=CGPointMake(((newWorldPoint.x*_world.xScale)*-1),(newWorldPoint.y*_world.yScale)*-1);
//为什么持续时间必须精确到0.3才能工作
SKAction*moveToCenter=[SKAction moveTo:alteredWorldCenter持续时间:0.3];
[_WorldRunAction:moveToCenter];
}
}否则,如果([发送方刻度]<\u临时刻度){
如果(_world.xScale>0.5&&u world.xScale>0.3){
//_world.xScale-=0.05;
//_world.yScale-=0.05;
//[_世界设置刻度:[发送方刻度]];
[_worldsetscale:_world.xScale-=0.05];
CGPoint screenCenter=CGPointMake(_initialScreenSize.width/2,_initialScreenSize.height/2);
CGPoint newWorldPoint=[self-convertTouchPointToWorld:screenCenter];
//疯狂的方法为什么这样做
CGPoint alteredWorldCenter=CGPointMake(((newWorldPoint.x*_world.xScale-_initialScreenSize.width)*-1),(newWorldPoint.y*_world.yScale-_initialScreenSize.height)*-1);
SKAction*moveToCenter=[SKAction moveTo:alteredWorldCenter持续时间:0.3];
[_WorldRunAction:moveToCenter];
}
}
}
if(sender.state==UIGestureRecognitizerStateEnded){
[_世界远程行动];
}
}

我已经尝试过很多次了,但正是这段代码让我最接近于抓住世界上的一点。不过也有一些问题。当你离中心越来越远时,它也不起作用,因为它仍然试图放大世界的中心。将中心点转换为世界节点后,我仍然需要再次操纵它以使其正确居中(我描述为疯狂的公式)。它必须是不同的放大和缩小工作。移动操作的持续时间必须设置为0.3,否则它几乎不起作用。更高或更低,它不会放大中心点。如果我尝试将缩放增加一小部分以上,它的移动速度会非常快。如果挤压结束时我不结束动作,屏幕就会抖动。我完全不明白为什么会这样(它在延迟结束和屏幕抖动之前平滑地放大到中心点),我不确定我做错了什么。非常感谢您的帮助

看看我对一个非常类似问题的回答

我发布的代码将缩放“锚定”在捏手势的位置,而不是屏幕的中心,但这很容易改变,因为我尝试了两种方法

根据下面评论中的要求,我也将我的平移代码添加到这个答案中

平移代码

// instance variables of MyScene.
SKNode *_mySkNode;
UIPanGestureRecognizer *_panGestureRecognizer;

- (void)didMoveToView:(SKView *)view
{
    _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];
    [[self view] addGestureRecognizer:_panGestureRecognizer];
}

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan) {

        [recognizer setTranslation:CGPointZero inView:recognizer.view];

    } else if (recognizer.state == UIGestureRecognizerStateChanged) {

        CGPoint translation = [recognizer translationInView:recognizer.view];
        translation = CGPointMake(-translation.x, translation.y);

        _mySkNode.position = CGPointSubtract(_mySkNode.position, translation);

        [recognizer setTranslation:CGPointZero inView:recognizer.view];

    } else if (recognizer.state == UIGestureRecognizerStateEnded) {

        // No code needed for panning.
    }
}
下面是上面使用的两个辅助函数。它们来自Ray Wenderlich关于精灵装备的书

SKT_INLINE CGPoint CGPointAdd(CGPoint point1, CGPoint point2) {
    return CGPointMake(point1.x + point2.x, point1.y + point2.y);
}

SKT_INLINE CGPoint CGPointSubtract(CGPoint point1, CGPoint point2) {
    return CGPointMake(point1.x - point2.x, point1.y - point2.y);
}

你在那一页上的回答确实非常有效。我对你感激不尽!你会发布你的平移代码吗?我很想看看你的方法,当然。平移实际上要容易得多。我刚刚在上面的答案中添加了平移代码。谢谢,帮了我很多忙!:D