1

我正在尝试使用 UIRrotationGestureRecognizer 旋转 SKSpriteNode。我已经实现了一个代码,但有时,当我旋转节点时,它会跳转到一个不应该的旋转。这里有代码:

- (void) handleRotation:(UIRotationGestureRecognizer *) rotationrecognizer{

    CGFloat initialrotation = 0.0;

    if (rotationrecognizer.state == UIGestureRecognizerStateBegan) {

      CGPoint touchLocation = [rotationrecognizer locationInView:rotationrecognizer.view];
      touchLocation = [self convertPointFromView:touchLocation];
      [self selectNodeForTouch:touchLocation];

      initialrotation = selected.zRotation;
    }

    else if (rotationrecognizer.state == UIGestureRecognizerStateChanged) {        

      CGFloat angle = initialrotation + rotationrecognizer.rotation;
      selected.zRotation = angle;      

   }   
}
4

1 回答 1

3

您在每次通话时将初始旋转重置为 0...如果您需要它保持驻留,您应该将其移动为您的视图的一个 ivar。

你现在写的方式,设置“角度”的行实际上等于:

CGFloat angle = 0 + rotationrecognizer.rotation;

相反,您应该执行以下操作(其中 initialRotation 定义为私有 ivar):

- (void) handleRotation:(UIRotationGestureRecognizer *) rotationrecognizer{

    if (rotationrecognizer.state == UIGestureRecognizerStateBegan) {

      CGPoint touchLocation = [rotationrecognizer locationInView:rotationrecognizer.view];
      touchLocation = [self convertPointFromView:touchLocation];
      [self selectNodeForTouch:touchLocation];

      _initialrotation = selected.zRotation;
    }

    else if (rotationrecognizer.state == UIGestureRecognizerStateChanged) {        

      CGFloat angle = _initialrotation + rotationrecognizer.rotation;
      selected.zRotation = angle;      

   }   
}
于 2013-09-30T17:37:58.093 回答