2

首先,我是 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;
}   
4

1 回答 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];
}
于 2013-05-23T15:19:55.287 回答