我正在尝试使用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;
}
}
答案 0 :(得分:3)
您正在每次通话时将初始轮换重置为0 ...如果您需要将其保留为居住地,您应该将其移动到您的视图的ivar。
现在你写的方式,设置'angle'的线实际上等于:
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;
}
}