【问题标题】:Rotating image round a fix point围绕固定点旋转图像
【发布时间】:2013-05-23 13:48:01
【问题描述】:

首先,我是 Objective C 的新手,任何帮助将不胜感激。我正在尝试编写一个 Gauge 应用程序(最终会显示手机的角度)。我遇到的问题是当我旋转针图像时,它旋转到正确的角度,但不是从锚点旋转 - 图像移动位置。即使当我移动时,比如从 0 渣到 90 再回到 0,图像也不会回到原来的位置!我正在使用的代码如下:. (PS 我目前只在模拟器上运行应用)

*** Rotate Invoked by ....
// Rotate Image
[self rotateImage:_imgNeedle duration: vAnimationDuration curve: UIViewAnimationCurveLinear degrees:vValue];

-(void)rotateImage:(UIImageView *)image
       duration:(NSTimeInterval)duration
          curve:(int)curve
        degrees:(CGFloat)degrees
{
  // Setup the animation
  [self setAnchorPoint:CGPointMake(.44,.85) forView:image];
  // image.transform = CGAffineTransformMakeRotation(d2r(degrees));

  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:duration];
  [UIView setAnimationCurve:curve];
  [UIView setAnimationBeginsFromCurrentState:YES];

  // The transform matrix
  CGAffineTransform transform = CGAffineTransformMakeRotation(d2r(degrees));

  image.transform = transform;
  // Commit the changes
  [UIView commitAnimations];
}

// Set Anchor Point
-(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view
{
  CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y);
  CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y);

  newPoint = CGPointApplyAffineTransform(newPoint, view.transform);
  oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);

  CGPoint position = view.layer.position;

  position.x -= oldPoint.x;
  position.x += newPoint.x;

  position.y -= oldPoint.y;
  position.y += newPoint.y;

  view.layer.position = position;
  view.layer.anchorPoint = anchorPoint;
}   

【问题讨论】:

    标签: iphone ios image-rotation


    【解决方案1】:

    图层位置我不确定,但是当我改变anchorPoint时,框架会改变,所以我会调整框架。

    -(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view
    {
      CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y);
      CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y);
    
      newPoint = CGPointApplyAffineTransform(newPoint, view.transform);
      oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);
      /* 
      CGPoint position = view.layer.position;
    
      position.x -= oldPoint.x;
      position.x += newPoint.x;
    
      position.y -= oldPoint.y;
      position.y += newPoint.y;
    
      view.layer.position = position;
      */
    
      view.frame = CGRectOffset(view.frame, newPoint.x-oldPoint.x, newPoint.y-oldPoint.y);
      view.layer.anchorPoint = anchorPoint;
    }   
    

    而且如果你想通过多次调用rotateImage回到原来的位置,我认为你应该只在此之前设置一次锚点。因为如果您使用相同的 rotateImage 方法制作两个单独的动画,锚点将使用您当前的实现重新计算。

    -(void)rotateImage:(UIImageView *)image
           duration:(NSTimeInterval)duration
              curve:(int)curve
            degrees:(CGFloat)degrees
    {
      // Setup the animation
      // [self setAnchorPoint:CGPointMake(.44,.85) forView:image];
      // image.transform = CGAffineTransformMakeRotation(d2r(degrees));
    
      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationDuration:duration];
      [UIView setAnimationCurve:curve];
      [UIView setAnimationBeginsFromCurrentState:YES];
    
      // The transform matrix
      CGAffineTransform transform = CGAffineTransformMakeRotation(d2r(degrees));
    
      image.transform = transform;
      // Commit the changes
      [UIView commitAnimations];
    }
    

    【讨论】:

    • 感谢您的回复。我进行了更改,还注意到采样率小于动画持续时间。但是虽然它更好,但不幸的是它仍然不太正确。在故事板上的设计中,指针位于表盘的中心,运行时它会垂直向上移动。改变角度,我得到了一些水平运动。
    • @user2413698 不确定您需要什么。也许您可以使用更多信息更新您的问题。如果此答案解决了您遇到的特定问题,或者发布另一个问题。
    • 关闭自动布局 - 针现在围绕轴正确旋转 - 已修复!
    • @user2413698 哦,酷:)。很高兴你设法修复它。如果我的回答是正确的,请采纳。
    猜你喜欢
    • 2011-11-16
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    相关资源
    最近更新 更多