将CGrect值更改为用户坐标系 [英] Changing CGrect value to user coordinate system

查看:171
本文介绍了将CGrect值更改为用户坐标系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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.

推荐答案

从坐标系统转换矩形,该坐标系的原点位于左下角(让我们调用它传统的坐标系统给它名字)到一个系统,其中原点在左上角(iPhone坐标系),你需要知道视图的大小,其中 CGRect 是,因为视图是矩形的引用。

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.

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

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

所以我们会有像 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)

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

这篇关于将CGrect值更改为用户坐标系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆