【发布时间】:2014-03-05 14:02:42
【问题描述】:
我在 UIView 中使用 CoreGraphics 来绘制图形,我希望能够使用触摸输入与图形进行交互。由于在设备坐标中接收到触摸,因此我需要将其转换为用户坐标以便将其与图形相关联,但这已成为一个障碍,因为 CGContextConvertPointToUserSpace 在图形绘制上下文之外不起作用。
这是我尝试过的。
在drawRect中:
CGContextScaleCTM(ctx,...);
CGContextTranslateCTM(ctx,...); // transform graph to fit the view nicely
self.ctm = CGContextGetCTM(ctx); // save for later
// draw points using user coordinates
在我的触摸事件处理程序中:
CGPoint touchDevice = [gesture locationInView:self]; // touch point in device coords
CGPoint touchUser = CGPointApplyAffineTransform(touchDevice, self.ctm); // doesn't give me what I want
// CGContextConvertPointToUserSpace(touchDevice) <- what I want, but doesn't work here
使用 ctm 的倒数也不起作用。我承认我很难理解设备坐标、用户坐标和转换矩阵之间的含义和关系。我认为这并不像我想要的那么简单。
编辑:来自 Apple 文档(iOS Coordinate Systems 和 Drawing Model)的一些背景。
“窗口在屏幕坐标中定位和调整大小,屏幕坐标由显示器的坐标系定义。”
“绘图命令引用一个固定比例的绘图空间,称为用户坐标空间。操作系统将这个绘图空间中的坐标单位映射到相应目标设备的实际像素上。 "
“您可以通过修改当前变换矩阵 (CTM) 来更改视图的默认坐标系。CTM 将视图坐标系中的点映射到设备屏幕上的点。”
【问题讨论】:
-
不明白..屏幕坐标不是用户坐标?我很困惑
-
我删除了我的答案(它得到了屏幕坐标),因为我显然弄错了——感谢一些见解
-
感谢您的帮助。我也很困惑!查看我的编辑。
-
@Daij-Djan 希望我下面的回答有意义!
标签: ios objective-c core-graphics coordinate-systems coordinate-transformation