首页 文章

围绕固定点旋转图像

提问于
浏览
2

首先,我是Objective C的新手,任何帮助都将不胜感激 . 我试着写一个Gauge应用程序(最终会显示手机的角度) . 我的问题是当我旋转Needle图像时,它旋转到正确的角度,但不是从Anchor点旋转 - 图像移动位置 . 即使我移动,从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;
}

1 回答

  • 0

    我不确定图层位置,但是当我更改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];
    }
    

相关问题