我正在重新介绍自己的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);
图像确实已旋转,但中心似乎不是旋转的锚点。我一直在谷歌搜索,但无法弄清楚。有人能指出我正确的方向吗?
这是一个屏幕截图,以防它不清楚:在初始状态下,标签位于圆圈的中心,白色方块位于图像的中心位置:
答案 0 :(得分: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];
答案 1 :(得分:0)
Greg代码的快速端口
let rotationTransform: CATransform3D = CATransform3DMakeRotation(.pi, 0, 0, 1.0);
let rotationAnimation = CABasicAnimation(keyPath: "transform");
rotationAnimation.toValue = NSValue(caTransform3D: rotationTransform)
rotationAnimation.duration = 0.6;
rotationAnimation.isCumulative = true;
rotationAnimation.repeatCount = Float.infinity;
progressArc.layer.add(rotationAnimation, forKey:"rotationAnimation")
答案 2 :(得分:-1)
为您的案例尝试此代码
var angle=0;
self.btnImage.center=CGPointMake(self.btnImage.center.x, self.btnImage.center.y);
self.btnImage.transform=CGAffineTransformMakeRotation (angle);
angle+=0.15;
将上述方法保留为方法,使用时间间隔或在想要更改图像角度时调用方法。