Ios 目标c-旋转图像的仿射旋转

Ios 目标c-旋转图像的仿射旋转,ios,objective-c,rotation,Ios,Objective C,Rotation,我一直试图让UIImageView在我触摸和拖动某个点时围绕该点旋转。如果我再次尝试旋转它,它会跳回原来的位置。我已经尝试过这个解决方案,但这会让图像疯狂旋转 我做错了什么?这是我的密码: 更新了带有修复程序的代码: -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint location

我一直试图让UIImageView在我触摸和拖动某个点时围绕该点旋转。如果我再次尝试旋转它,它会跳回原来的位置。我已经尝试过这个解决方案,但这会让图像疯狂旋转

我做错了什么?这是我的密码:

更新了带有修复程序的代码:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];

 if (CGRectContainsPoint(Game10Wheel.frame, location)) {
        Game10Angle = atan2([Game10Wheel center].y - location.y, [Game10Wheel center].x - location.x);
        Game10WheelTouched = true;
    }

}


-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
   if (Game10WheelTouched) {
     UITouch *touch = [[event allTouches] anyObject];
     CGPoint location = [touch locationInView:touch.view];

      float theAngle = atan2([Game10Wheel center].y - location.y, [Game10Wheel center].x - location.x);
      [Game10Wheel setTransform: CGAffineTransformRotate([Game10Wheel transform], theAngle - Game10Angle)];
      Game10Angle = theAngle;
  }
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  Game10WheelTouched = false;
}

-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  Game10WheelTouched = false;
}

谢谢

您需要修改对象的当前变换,否则您只是将其设置为初始值。使用
CGAffineTransformMakeRotation
创建一个新的变换对象,仅对其应用指定的旋转。您要做的是使用
cGraffineTransformRotate
并传递
Game10Wheel
对象的当前变换,以将旋转添加到当前变换中


编辑:换句话说,当您创建旋转时,它应该从
Game10Wheel
的当前变换开始,而不是从新变换开始

您在计算进一步变换的旋转角度时遇到的问题。尝试使用下面的代码

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [[event allTouches] anyObject];
  CGPoint location = [touch locationInView:touch.view];
  float theAngle = atan2([Game10Wheel center].y - location.y, [Game10Wheel center].x - location.x);
  [Game10Wheel setTransform: CGAffineTransformRotate([Game10Wheel transform], theAngle - Game10Angle)];
  Game10Angle = theAngle;
}

谢谢你的回复!即使换了零钱,轮子还是会疯狂地旋转。我已经更新了OP中的代码,我做错了什么?试着减少应用每帧的旋转量。谢谢,这很有效!我现在唯一的问题是,它会跳到你触摸它的任何地方,你有什么想法可以防止这种情况发生吗?@originaluser2跳跃发生在你开始触摸时?如果“是”-发布你的
touchesbearth
方法。我已经用touchesbearth方法更新了OP,除了在移动轮子之前检查轮子是否被触碰之外,里面什么都没有,但这不会影响它跳跃。@originaluser2在提供的代码中,我没有看到任何明显的问题。检查所有改变物体或物体框架中心坐标的方法。啊,明白了!当触摸开始时,我只需要将Game10Angle重置为当前角度:
Game10Angle=atan2([Game10Wheel center].y-location.y[Game10Wheel center].x-location.x)谢谢你的帮助!