滑动时主动旋转SKSpriteNode?

时间:2014-06-18 15:17:18

标签: ios sprite-kit uigesturerecognizer skspritenode

如果用户向左或向右滑动,我想要旋转SKSpriteNode。 我可以使用UIGestureRecognizer还是有另一种方法来解决它?

2 个答案:

答案 0 :(得分:0)

最简单的方法是向SKView添加两个UISwipeGestureRecognizer(每个方向一个)。

如果你想处理更慢的手指动作,你需要用UIPanGestureRecognizer替换它们。

答案 1 :(得分:0)

在你的场景中,像这样实施didMoveToView:(SKView*)view

- (void)didMoveToView:(SKView *)view {
    self.leftSwipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipe)];
    self.leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [view addGestureRecognizer:self.leftSwipeRecognizer];

    self.rightSwipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipe)];
    self.rightSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [view addGestureRecognizer:self.rightSwipeRecognizer];
}

- (void)rightSwipe {

    //whatever angle and duration you need here  (angle in radians)
    NSTimeInterval duration = 0;
    CGFloat angle = 0;

    [self enumerateChildNodesWithName:nodeIdentifier usingBlock:^(SKNode *node, BOOL *stop) {
        [node runAction:[SKAction rotateByAngle:angle duration:duration]];
    }];
}
  • 必须将节点设置为场景的子节点。
  • 此处的nodeIdentifier将是您要旋转的节点的名称。这应该设置为您希望移动的name的{​​{1}}属性。