【问题标题】:CGAffineTransformMakeRotation stretches my image, what am I doing wrong?CGAffineTransform Make Rotation 拉伸了我的图像,我做错了什么?
【发布时间】:2011-06-22 10:22:09
【问题描述】:

我有一张图片,显示地图上哪条路是北方。如果找到新标题,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;

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

【问题讨论】:

    标签: iphone objective-c transform


    【解决方案1】:

    另一种解决方案:

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

    【讨论】:

    • 在开始时将转换设置为身份的绝佳建议。如果您在 -layoutSubviews 中设置转换并且您的 layoutSubviews 被调用两次,这将特别有用。
    • 仅添加这一行就解决了我的问题 locateMeView.transform = CGAffineTransformIdentity;谢谢
    【解决方案2】:

    我发现出了什么问题(无论如何或多或少)。使用 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));
    

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

    【讨论】:

    • 这让我发疯了。感谢分享。
    • here 所述,您也可以使用bounds 属性。
    • 不幸的是,即使您不改变框架或位置,这种失真仍然会发生。
    • @Oscar:将ContentMode 设置为UIViewContentMode.Center 可能会解决您的问题。
    • +1,这个答案给了我足够的提示,我记得在顶级视图中关闭自动布局。谢谢!
    【解决方案3】:

    尝试在调整大小后设置变换?顺序对转换很重要。

    【讨论】:

    • 我尝试了所有不同的订单,但正如您在几个小时后看到的那样,我找到了解决方案。
    • 你好!感谢分享。
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 2021-04-19
    相关资源
    最近更新 更多