将iOS设备坐标转换为drawRect外部的用户坐标

将iOS设备坐标转换为drawRect外部的用户坐标,ios,objective-c,core-graphics,coordinate-systems,coordinate-transformation,Ios,Objective C,Core Graphics,Coordinate Systems,Coordinate Transformation,我在UIView中使用CoreGraphics绘制图形,我希望能够使用触摸输入与图形交互。由于触摸是在设备坐标中接收的,因此我需要将其转换为用户坐标,以便将其与图形关联起来,但这已经成为一个障碍,因为CGContextConvertPointToUserSpace在图形绘制上下文之外不起作用 这是我试过的 在drawRect中: CGContextScaleCTM(ctx,...); CGContextTranslateCTM(ctx,...); // transform graph

我在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
CGAffineTransform scale = CGAffineTransformMakeScale(...);
CGAffineTransform translate = CGAffineTransformMakeTranslation(...);
self.myTransform = CGAffineTransformConcat(translate, scale);
// draw points using user coordinates
在my touch事件处理程序中:

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
CGPoint touch = [gesture locationInView:self]; // touch point in view coords
CGPoint touchUser = CGPointApplyAffineTransform(touchPoint, CGAffineTransformInvert(self.myTransform)); // this does the trick
#import <QuartzCore/QuartzCore.h>

CGPoint touch = [gesture locationInView:self]; // view coords

CGSize layerSize = [self.layer frame].size;
UIGraphicsBeginImageContext(layerSize);
CGContextRef context = UIGraphicsGetCurrentContext();

// as in drawRect:
CGContextScaleCTM(...); 
CGContextTranslateCTM(...);

CGPoint touchUser = CGContextConvertPointToUserSpace(context, touch); // now it gives me what I want

UIGraphicsEndImageContext();
CGPoint touch device=[手势位置查看:self];//设备坐标中的接触点
CGPoint touchUser=cgpointapplyAffinetTransform(touchDevice,self.ctm);//没有给我想要的
//CGContextConvertPointToUserSpace(touchDevice)我发现CTM已经包含了从地图视图坐标(原点在左上角)到屏幕坐标(原点在左下角)的转换。所以(0,0)被转换为(0800),其中我的视图高度为800,(0,2)映射到(0798)等等。所以我收集了我们正在讨论的3个坐标系:屏幕坐标视图/设备坐标用户坐标。(如果我错了,请纠正我。)

CGContext转换(CTM)从用户坐标一直映射到屏幕坐标。我的解决方案是单独维护自己的转换,该转换将用户坐标映射到视图坐标。然后我可以使用它从视图坐标返回用户坐标

我的解决方案:

在drawRect中:

CGContextScaleCTM(ctx,...);     
CGContextTranslateCTM(ctx,...); // transform graph to fit the view nicely
self.ctm = CGContextGetCTM(ctx); // save for later
// draw points using user coordinates
CGAffineTransform scale = CGAffineTransformMakeScale(...);
CGAffineTransform translate = CGAffineTransformMakeTranslation(...);
self.myTransform = CGAffineTransformConcat(translate, scale);
// draw points using user coordinates
在my touch事件处理程序中:

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
CGPoint touch = [gesture locationInView:self]; // touch point in view coords
CGPoint touchUser = CGPointApplyAffineTransform(touchPoint, CGAffineTransformInvert(self.myTransform)); // this does the trick
#import <QuartzCore/QuartzCore.h>

CGPoint touch = [gesture locationInView:self]; // view coords

CGSize layerSize = [self.layer frame].size;
UIGraphicsBeginImageContext(layerSize);
CGContextRef context = UIGraphicsGetCurrentContext();

// as in drawRect:
CGContextScaleCTM(...); 
CGContextTranslateCTM(...);

CGPoint touchUser = CGContextConvertPointToUserSpace(context, touch); // now it gives me what I want

UIGraphicsEndImageContext();
替代解决方案:

另一种方法是手动设置相同的上下文,但我认为这更像是一种黑客行为

在my touch事件处理程序中:

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
CGPoint touch = [gesture locationInView:self]; // touch point in view coords
CGPoint touchUser = CGPointApplyAffineTransform(touchPoint, CGAffineTransformInvert(self.myTransform)); // this does the trick
#import <QuartzCore/QuartzCore.h>

CGPoint touch = [gesture locationInView:self]; // view coords

CGSize layerSize = [self.layer frame].size;
UIGraphicsBeginImageContext(layerSize);
CGContextRef context = UIGraphicsGetCurrentContext();

// as in drawRect:
CGContextScaleCTM(...); 
CGContextTranslateCTM(...);

CGPoint touchUser = CGContextConvertPointToUserSpace(context, touch); // now it gives me what I want

UIGraphicsEndImageContext();
#导入
CGPoint touch=[手势位置视图:self];//视图坐标
CGSize layerSize=[self.layer frame].size;
UIGraphicsBeginImageContext(layerSize);
CGContextRef context=UIGraphicsGetCurrentContext();
//如drawRect所示:
CGContextScaleCTM(…);
CGContextTranslateCm(…);
CGPoint touchUser=CGContextConvertPointToUserSpace(上下文,触摸);//现在它给了我想要的
UIGraphicsSendImageContext();

不明白。。屏幕坐标不是用户坐标?我很困惑我删除了我的答案(得到了屏幕坐标),因为我显然弄错了——我很感激你的一些见解。我很感谢你的帮助。我也很困惑!请看我的编辑。@Daij Djan希望我下面的回答有意义!