CGAffineTransformMakeRotation拉伸了我的形象,我做错了什么?

Vin*_*nga 8 iphone transform objective-c

我有一张图片,显示地图上哪条路是北方.如果找到新标题,则此图像由CoreLocation更新.它也放在正确的位置,我使用以下代码:

locateMeView.transform = CGAffineTransformMakeRotation(([CoreLocationController sharedInstance].heading - 45) * M_PI / 180);
CGRect frame = locateMeView.frame;
CGPoint pos = [MapClass vectorForLocation:[CoreLocationController sharedInstance].location mapCenter:curLocation zoom:curZoom];
frame.origin.x = (int)(self.view.bounds.size.width/2 + pos.x - frame.size.width / 2);
frame.origin.y = (int)(self.view.bounds.size.height/2 + pos.y - frame.size.height / 2);
frame.size.width = 24;
frame.size.height = 24;
locateMeView.frame = frame;
Run Code Online (Sandbox Code Playgroud)

CoreLocationController是一个存储正常CoreLocation更新的类.MapClass将lat/lng坐标转换为地图上的x/y坐标.图像的位置是正确的,但旋转会产生奇怪的效果.对于0 en M_PI,图像是正确的,但在这些图像之间,图像被拉伸,好像它也围绕z轴旋转,并且在M_PI/2和3*M_PI/2处,它完全消失.有人可以解释发生了什么,我做错了什么?

Vin*_*nga 14

我发现了什么是错的(或多或少).使用transform属性时,不允许(无论出于何种原因)通过更改框架来更改位置,您必须使用center属性.所以最后的代码是:

CGPoint pos = [MapClass vectorForLocation:[CoreLocationController sharedInstance].location mapCenter:curLocation zoom:curZoom];
locateMeView.transform = CGAffineTransformMakeRotation(([CoreLocationController sharedInstance].heading - 45) * M_PI / 180);
locateMeView.center = CGPointMake(int)(self.view.bounds.size.width/2 + pos.x), (int)(self.view.bounds.size.height/2 + pos.y));
Run Code Online (Sandbox Code Playgroud)

希望有同样问题的人能找到答案.

  • 不幸的是,即使你没有改变框架或位置,这种失真仍然会发生. (5认同)
  • @Oscar:将`ContentMode`设置为`UIViewContentMode.Center`可以解决您的问题. (2认同)

Hiv*_*cks 6

另一种方案:

  1. 将变换设置为标识
  2. 改变框架
  3. 设置变换

在你的情况下应该是:

locateMeView.transform = CGAffineTransformIdentity;

CGRect frame = locateMeView.frame;
CGPoint pos = [MapClass vectorForLocation:[CoreLocationController sharedInstance].location mapCenter:curLocation zoom:curZoom];
frame.origin.x = (int)(self.view.bounds.size.width/2 + pos.x - frame.size.width / 2);
frame.origin.y = (int)(self.view.bounds.size.height/2 + pos.y - frame.size.height / 2);
frame.size.width = 24;
frame.size.height = 24;
locateMeView.frame = frame;

locateMeView.transform = CGAffineTransformMakeRotation(([CoreLocationController sharedInstance].heading - 45) * M_PI / 180);
Run Code Online (Sandbox Code Playgroud)

  • 建议在开始时将变换置于身份的极好建议.如果您在-layoutSubviews中设置转换并且您的layoutSubviews被调用两次,这将非常有用. (2认同)