ios - 使用 SpriteKit 手势同时分别拖动、旋转和缩放多个 Sprite

标签 ios sprite-kit gestures

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

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

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

例如,对于 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];

}

最后是移动 Sprite 的方法:

- (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];

     }

关于ios - 使用 SpriteKit 手势同时分别拖动、旋转和缩放多个 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22826246/

相关文章:

objective-c - 如何使用EventKit Framework提前一年设置提醒

ios - 为什么 Swift 中 CFStringEncodings 没有 UTF8?

ios - Swizzling 符合 UI_APPEARANCE_SELECTOR 的属性

gestures - cocos3d 广告牌平移手势

android - 尝试将手势保存到 GestureLibrary 时出现问题

ios - 如何在 iOS 6.1 上正确设置 GKSession(蓝牙)

ios - 无法通过触摸按钮加载新的 SpriteKit SKScene

ios - 当设备笔直且不倾斜时,如何让我的加速度计不移动我的节点?

ios - 限制 Spritekit 游戏中 GUI 元素的比例

android - 试图刷掉 native FlatList 中的项目但没有收到 onResponderTerminationRequest 事件