【发布时间】:2019-01-01 10:07:41
【问题描述】:
在macOS编程中,我们知道
- Quartz 使用原点 (0, 0) 位于主显示屏左上角的坐标空间。增加 y 会下降。
- Cocoa 使用坐标空间,其中原点 (0, 0) 位于主显示屏的左下角,并且 y 增加向上。
现在我正在使用 Quartz API - CGImageCreateWithImageInRect 来裁剪图像,该图像将矩形作为参数。矩形的 Y 原点来自 Cocoa 的 mousedown 事件。
因此我在倒置的位置收获作物......
我试过这段代码来翻转我的cropRect中的Y坐标
//Get the point in MouseDragged event
NSPoint currentPoint = [self.view convertPoint:[theEvent locationInWindow] fromView:nil];
CGRect nsRect = CGRectMake(currentPoint.x , currentPoint.y,
circleSizeW, circleSizeH);
//Now Flip the Y please!
CGFloat flippedY = self.imageView.frame.size.height - NSMaxY(nsRectFlippedY);
CGRect cropRect = CGRectMake(currentPoint.x, flippedY, circleSizeW, circleSizeH);
但是对于顶部的区域,我错误的 FlippedY 坐标。 如果我在视图的顶部边缘附近单击,我会翻转 Y = 510 到 515 在顶部边缘,它应该在 0 到 10 之间:-|
谁能指出正确可靠的翻转方式 在这种情况下的 Y 坐标?谢谢!
这是 GitHub 中突出显示问题的示例项目 https://github.com/kamleshgk/SampleMacOSApp
【问题讨论】:
-
当你说“矩形的 Y 原点来自 Cocoa 的 mousedown 事件”时,你的意思是
-locationInWindow?也就是说,顾名思义,是相对于窗口的坐标,而不是屏幕坐标。 -
什么是
nsRect? -
@KenThomases 是的,来自点的 Y 来自此代码 - NSPoint currentPoint = [self.view convertPoint:[theEvent locationInWindow] fromView:nil];
-
@Willeke nsRect 是包含此代码中 Y 点的矩形 NSPoint currentPoint = [self.view convertPoint:[theEvent locationInWindow] fromView:nil];
-
@Willeke 请参阅 nsRect 问题中的更新代码!谢谢!
标签: macos cocoa quartz-graphics