NSRect vs CGRect:y轴反转 [英] NSRect vs CGRect: y-axis inversion

查看:61
本文介绍了NSRect vs CGRect:y轴反转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试将NSRect转换为CGRect.我使用了 NSRectToCGRect(),它可以很好地复制所有内容,但没有考虑y轴.

So I'm trying to convert an NSRect to a CGRect. I used NSRectToCGRect() which copies everything over finely, but does not take into account y-origin axis.

问题:(0,0)的CGRect原点位于左上方.NSRect的原点(0,0)在左下角.

The problem: A CGRect origin of (0,0) is the top left. NSRect origin (0,0) is bottom left.

因此, NSRect(0,0,100,100)框位于屏幕的左下方,而 CGRect(0,0,100,100)框位于屏幕的左上方屏幕左侧.

Thus an NSRect (0,0,100,100) box is positioned at the bottom left of your screen, while a CGRect (0,0,100,100) box is placed at the top left of your screen.

我有一个可以通过基本数学解决y起源的技巧:

I have a hack that fixes the y-origin through basic math:

fixedOriginY = screenHeight - NSRect.size.height - NSRect.origin.y

因此,等效于 NSRect(0,0,100,100)实际上是 CGRect(0,800,100,100)(在我900像素高的MBA上).但是我不喜欢这样,我感觉它会打破/出现视网膜问题或将来的并发症.

Thus the equivalent of an NSRect (0,0,100,100) is actually CGRect (0,800,100,100) (on my 900px height MBA). However I don't like this, I have a feeling it will break/have issues with retina or future complications.

有人对如何将NSRect正确转换为CGRect有解决方案或想法吗?

Does anyone have a solution or idea on how to correctly convert NSRect to CGRect?

推荐答案

矩形的原点在其左上角还是左下角取决于使用该矩形的坐标系.它不取决于您使用 CGRect 还是 NSRect .

Whether the origin of a rectangle is at its top left or bottom left depends on the coordinate system in which you use the rectangle. It does not depend on whether you use CGRect or NSRect.

通常,AppKit使用以左下角为原点的坐标系.同样,您创建的 CGContext 会将原点放在左下方.

Typically, AppKit uses coordinate systems where the lower-left corner is the origin. Similarly, a CGContext that you create puts the origin at the lower left.

另一方面,Quartz Event Services和Quartz Display Services将原点放在左上方.UIKit(在iOS上)将原点放在左上方.您可以在 NSView 子类(在OS X上)中覆盖 isFlipped ,以将其起源放在左上方.

On the other hand, Quartz Event Services and Quartz Display Services put the origin at the upper left. UIKit (on iOS) puts the origin at the upper left. You can override isFlipped in an NSView subclass (on OS X) to put its origin at the upper left.

将矩形从传统的(左下原点)坐标系转换为翻转的(左上原点)坐标系时,您的基本数学知识是必要且适当的.

Your basic math is necessary and proper when you are converting a rectangle from a traditional (lower-left origin) to flipped (upper-left origin) coordinate system.

这篇关于NSRect vs CGRect:y轴反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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