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

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

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

这是一个屏幕截图,以防不清楚:在初始状态下,标签在圆圈中居中,白色正方形在图像中居中:

最佳答案

此代码适用于我:

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];

关于ios - 如何围绕自己的中心旋转UIImageView?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21630191/

10-12 04:21