2009-11-15 81 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