将 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要将矩形从原点位于左下角的坐标系(我们将其称为传统坐标系来命名)转换为原点位于左上角的系统(iPhone 坐标系),您需要知道
CGRect
所在视图的大小,因为视图是矩形的引用。例如,假设您有一个尺寸为 200 x 300 的视图,并且您拥有的
CGRect
为CGRectMake(10, 20, 30, 40)
。CGRect
的大小将保持不变。所改变的只是原点 - 实际上,只有 y 坐标会改变,而 x 坐标不会改变,因为传统坐标系和 iPhone 坐标系都从左侧开始(一个在左下角,另一个在左上角)。因此,我们将得到类似于 CGRectMake(10, y, 30, 40) 的内容。
从 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 isCGRectMake(10, 20, 30, 40)
. The size of theCGRect
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)
.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