繁体   English   中英

使用SpriteKit手势拖动,同时分别旋转和缩放多个精灵

[英]Using gestures with SpriteKit to Drag, rotate and scale multiple sprite separately at the same time

我创建了一个iOS应用程序,我需要能够同时移动,旋转和缩放精灵(我正在使用Apple的Sprite Kit)。 在大多数情况下,我有这个工作。 我目前可以用1个手指触摸并移动精灵,如果我用两个手指,我可以缩放和旋转精灵。 为此,我使用UIPanGestureRecognizer,UIPinchGestureRecognizer和UIRotateGestureRecognizer。 这很好。 我想要的是,当我拖动时,用右手旋转和缩放一个精灵,我可以用左手拖动旋转并缩放另一个精灵,与其他精灵无关。

目前我正在使用iOS手势来移动,旋转和缩放精灵。 我使用的代码非常接近我在Ray Wenderlich的网站上发现的Sprite Kit的Drag and Drop Sprites教程。 http://www.raywenderlich.com/44270/sprite-kit-tutorial-how-to-drag-and-drop-sprites 当我开始使用UIPanGestureRecognizers而不仅仅是触摸方法时,我正在使用的部分接近底部。

就像我说的那样,手势一次可以在一个精灵上正常工作。 如何使它适用于多个精灵?

例如,对于UIPanGesturRecognizer,我添加以下代码:

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

}

然后我有一个名为gestureRecognizer的方法如下:

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

    CGPoint touchLocation = [recognizer locationInView:recognizer.view];

    touchLocation = [self convertPointFromView:touchLocation];

    [self selectNodeForTouch:touchLocation]; // This just returns the node that has been touched


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

    CGPoint translation = [recognizer translationInView:recognizer.view];
    translation = CGPointMake(translation.x, -translation.y);
    [self panForTranslation:translation];
    [recognizer setTranslation:CGPointZero inView:recognizer.view];

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

   [_selectedNode removeAllActions];

}

}

最后有移动精灵的方法:

- (void)panForTranslation:(CGPoint)translation {

if([[_selectedNode name] isEqualToString:kAnimalNodeName]) {

    CGPoint position = [_selectedNode position];
    // Set variable for the point to move selected node
    CGPoint movePoint = CGPointMake(position.x + translation.x, position.y + translation.y);
    [_selectedNode setPosition:newPos];

}

}

现在示例代码仅显示UIPanGestureRecognizer的方法,但我也有类似的旋转和捏合手势方法。 所有这些代码都在我的场景类中。

感谢您的帮助。

好吧,你发布的教程几乎说明了如何做到这一点......

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    [self selectNodeForTouch:positionInScene];
}

- (void)selectNodeForTouch:(CGPoint)touchLocation {
    //The below statement assigns touchedNode from a sprite that contains touchLocation
    SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation];

  //2
if(![_selectedNode isEqual:touchedNode]) {
    [_selectedNode removeAllActions];
    [_selectedNode runAction:[SKAction rotateToAngle:0.0f duration:0.1]];

    _selectedNode = touchedNode;

        //the below if statement determines what SKNode is to be given a SKAction
    if([[touchedNode name] isEqualToString:kAnimalNodeName]) {
    SKAction *sequence = [SKAction sequence:@[[SKAction rotateByAngle:degToRad(-4.0f) duration:0.1],
                                                  [SKAction rotateByAngle:0.0 duration:0.1],
                                                  [SKAction rotateByAngle:degToRad(4.0f) duration:0.1]]];
        [_selectedNode runAction:[SKAction repeatActionForever:sequence]];
        }
    }
}

因此,如果要将操作应用于多个节点,只需为它们指定相同的名称即可。 我建议命名你的节点,将它们放在一个数组中,然后迭代它们检查它们是否具有相同的名称。 如果您有任何其他问题,请发表评论。

更新:1

- (void)panForTranslation:(CGPoint)translation {

    //Once again you would do the same thing. Just give the nodes the same name. 
    if([[_selectedNode name] isEqualToString:kAnimalNodeName]) {

        CGPoint position = [_selectedNode position];
        // Set variable for the point to move selected nodes
        CGPoint movePoint = CGPointMake(position.x + translation.x, position.y + translation.y);
        [_selectedNode setPosition:newPos];

     }

我也在使用该教程做类似的事情。 示例代码依赖于实例变量selectedNode ,该变量表示selectedNode的一个节点。

为了使这个工作适用于多个节点,我建议使用selectedNodes的NSMutableArray ,或者子类化SKSpriteNode来存储它当前是否“被选中”。 祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM