获取触摸坐标(以0-480和0-320的形式)

jac*_*cky 5 iphone touch coordinate-systems

我不知道如何翻译iPhone的坐标.

如果我单击一下我就会得到坐标.

如果我触摸并保持并释放它,它会显示起点和终点的差异.

我如何获得触摸的绝对位置?

谢谢!

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
    NSLog(@"X: %f",location.x);
    NSLog(@"Y: %f",location.y);
}
Run Code Online (Sandbox Code Playgroud)

我想通过触摸和拖动来调整图像大小(仅限高度)

nas*_*ash 11

触摸的位置相对于视图计算.

如果你想要相对于屏幕的位置做:

UITouch * touch = [touches anyObject];
CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];
NSLog(@"Position of touch: %.3f, %.3f", pos.x, pos.y);
Run Code Online (Sandbox Code Playgroud)

(干编码,可能会出错)

  • 是不是[touch locationInView:nil]给出了相同的结果?文档说:"通过nil来获取窗口坐标中的触摸位置." (2认同)