将 CGrect 值更改为用户坐标系

发布于 2024-10-27 21:02:17 字数 91 浏览 1 评论 0原文

我有一个CGRect;我可以将其坐标转移到用户坐标系中,即左下角到顶部而不是左上角到底部。是否有任何预定义的方法或者我需要手动计算?提前致谢。

I have a CGRect; can I transfer its coordinates to be in the user coordinate system, i.e., bottom left corner to top instead of top left corner to bottom. Is there any predefined method for this or do I need to calculate it manually? Thanks in Advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的巷 2024-11-03 21:02:17

要将矩形从原点位于左下角的坐标系(我们将其称为传统坐标系来命名)转换为原点位于左上角的系统(iPhone 坐标系),您需要知道CGRect 所在视图的大小,因为视图是矩形的引用。

例如,假设您有一个尺寸为 200 x 300 的视图,并且您拥有的 CGRectCGRectMake(10, 20, 30, 40)CGRect 的大小将保持不变。所改变的只是原点 - 实际上,只有 y 坐标会改变,而 x 坐标不会改变,因为传统坐标系和 iPhone 坐标系都从左侧开始(一个在左下角,另一个在左上角)。

因此,我们将得到类似于 CGRectMake(10, y, 30, 40) 的内容。

  - (CGRect)rectangle:(CGRect)oldRect fromTraditionalToiPhoneCoordinatesWithReferenceViewOfSize:(CGSize)aSize
     {
      CGFloat oldY = oldRect.origin.y;  // This is the old y measured from the bottom left. 
      CGFloat newY = aSize.height - oldY - oldRect.size.height;

      CGRect newRect = oldRect;
      newRect.origin.y = newY;

      return newRect;
     }
    

从 iPhone(左上)坐标系测量的新矩形将为:CGRectMake(10, 300 - 20 - 40, 30, 40) = CGRectMake(10, 240, 30, 40)。

希望这张图片能让你更清楚
希望这张图片能让它更清晰

To convert a rectangle from a coordinate system that has its origin at the bottom left (let's just call it traditional coordinate system to give it name) to a system where the origin is at the top left (iPhone coordinate system) you need to know the size of the view where the CGRect is, since the view is the reference for the rectangle.

For example, let's say you have a view with size 200 x 300, and the CGRect you have is CGRectMake(10, 20, 30, 40). The size of the CGRect will remain the same. All that will change is the origin - actually, only the y coordinate will change and not the x coordinate, because the traditional and iPhone coordinate systems both start on the left side (one at the bottom left; the other at the top left).

So we will have something like CGRectMake(10, y, 30, 40).

  - (CGRect)rectangle:(CGRect)oldRect fromTraditionalToiPhoneCoordinatesWithReferenceViewOfSize:(CGSize)aSize
     {
      CGFloat oldY = oldRect.origin.y;  // This is the old y measured from the bottom left. 
      CGFloat newY = aSize.height - oldY - oldRect.size.height;

      CGRect newRect = oldRect;
      newRect.origin.y = newY;

      return newRect;
     }
    

The new rectangle, as measured from an iPhone (top left) coordinate system, would be: CGRectMake(10, 300 - 20 - 40, 30, 40) = CGRectMake(10, 240, 30, 40).

Hopefully this image makes it clearer
Hopefully this image makes it clearer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文