2013-07-01 39 views
0

我正在使用此方法,但它僅適用於我的UIView子類。如果我點擊UITextView或UIButton或任何其他對象,它不是顯示座標。如何獲取UITextView的點擊座標?爲什麼發生這種情況?如何獲取水龍頭的座標UITextView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *userTouch = [[event allTouches] anyObject]; 
    CGPoint currentPos = [userTouch locationInView:self.view]; 
    NSLog(@"Coordinates: (%f,%f)", currentPos.x, currentPos.y); 
} 
+1

你可能會需要繼承你的UITextView,並應用第二個答案在這裏找到:http://stackoverflow.com/questions/631427/is-there-a-way-to-pass-touches-through-on-the-iphone –

+0

它的工作原理,但它不會識別單個快速點擊(點擊)。我需要按下鼠標按鈕0.2-0.3秒,然後它完美地工作。你知道爲什麼嗎?延遲還是別的? –

+0

你使用滾動視圖嗎?有一個屬性delayedContentTouches,你會想要調整。 –

回答

0

添加在viewDidLoad方法此代碼,

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapRecognized:)]; 
singleTap.numberOfTapsRequired = 1; 
[self.textView addGestureRecognizer:singleTap]; 

實現方法,

- (無效)singleTapRecognized:(ID)發送方 {

CGPoint tapPoint = [sender locationInView:self.textView]; 
NSLog(@"Coordinates: (%f,%f)", tapPoint.x, tapPoint.y); 

}

+0

我應該將其添加到ViewController或UITextView? –

+0

添加到UITextView – karthika

相關問題