2014-02-07 41 views
0

我正在重新将自己介绍给iOS编程,并且遇到了一个看似简单的问题。我一直在关注this example about how to rotate a image according to the user's heading。关键部分是:如何在它自己的中心周围旋转UIImageView?

float heading = -1.0f * M_PI * newHeading.magneticHeading/180.0f; 
arrowImage.transform = CGAffineTransformMakeRotation(heading); 

图像确实是旋转的,但中心似乎不是旋转的锚点。我一直在谷歌搜索,但无法弄清楚。有人能指出我正确的方向吗?

下面是情况下的截图并不清楚:在初始状态,标签为中心的圆形和白色方块在图像中居中:

+0

您可以直接设置图层的锚点view.layer.anchorPoint = anchorPoint; – Jack

+0

anchorPoint **默认为**(0.5,0.5),也就是视图的中心。还有其他问题在这里,我们需要更多的细节。 –

+0

你需要什么样的细节? – Florian

回答

1

此代码适用于我:

UIImage *img = [UIImage imageNamed:@"Image.png"]; 
UIImageView *imageToMove = [[UIImageView alloc] initWithImage:img]; 

CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0); 

CABasicAnimation* rotationAnimation; 
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform]; 
rotationAnimation.duration = 0.6f; 
rotationAnimation.cumulative = YES; 
rotationAnimation.repeatCount = FLT_MAX; 

[imageToMove.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 
[self.view addSubview:imageToMove]; 
+0

你的x和y不需要以图像为中心吗?这应该仍然(如果文档是正确的)围绕图像的左上角旋转,而不是图像的中心。 – trumpetlicks

+0

虽然如此,但它的工作正常,它围绕图像的中心旋转。 – Greg

+0

有趣的知道:-) – trumpetlicks

-1

尝试此代码为您的情况

var angle=0; 

self.btnImage.center=CGPointMake(self.btnImage.center.x, self.btnImage.center.y); 
      self.btnImage.transform=CGAffineTransformMakeRotation (angle); 
      angle+=0.15; 

将上述方法保留在方法中,当您想要更改图像的角度时使用时间间隔或调用方法。

+1

这个答案太糟糕了,它伤害了。 –