2009-11-15 63 views
5

我没有得到如何翻译iphone的坐标。获取触摸坐标(以0-480和0-320的形式)

如果我做了一个单一的触摸我得到的坐标。

如果我触摸并保持并释放它,它会显示出发点和终点的区别。

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

谢谢!

-(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); 
} 

我想与

+0

您应该添加代码获取坐标 – Francescu 2009-11-15 12:09:40

回答

11

触摸的位置相对于一个视图来计算触摸和拖动(只有高度)调整的图像。

如果你想相对于屏幕的位置做到:(?继承的UIView)

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

(干编码,可能会给错误)

+0

谢谢! 如何通过拖动一边来调整UIImageVIew的大小? – jacky 2009-11-16 08:54:01

+0

我从来没有使用过UIImage类。查看UIImage类参考:http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html。更具体地说''drawInRect:' – nash 2009-11-16 09:41:15

+2

是不是[touch locationInView:nil]给出了相同的结果?该文档说:“通过零,以获得触摸位置在窗口的坐标。” – 2013-08-06 16:33:03