7
A
回答
30
有兩種方法可以完成此操作。如果你已經得到了你使用的UIView的子類,你可以重寫-touchesEnded:withEvent:
方法上的子類,像這樣:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *aTouch = [touches anyObject];
CGPoint point = [aTouch locationInView:self];
// point.x and point.y have the coordinates of the touch
}
如果你還沒有子類的UIView,雖然和視圖是由視圖控制器擁有或什麼的,那麼你可以使用一個UITapGestureRecognizer,像這樣:
// when the view's initially set up (in viewDidLoad, for example)
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
[someView addGestureRecognizer:rec];
[rec release];
// elsewhere
- (void)tapRecognized:(UITapGestureRecognizer *)recognizer
{
if(recognizer.state == UIGestureRecognizerStateRecognized)
{
CGPoint point = [recognizer locationInView:recognizer.view];
// again, point.x and point.y have the coordinates
}
}
2
2
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:myView];
NSLog("%lf %lf", touchPoint.x, touchPoint.y);
}
你需要做這樣的事情。 touchesBegan:withEvent:
是UIResponder
的一種方法,其中UIView
和UIViewController
都是從中導出的。如果你谷歌這種方法,那麼你會發現幾個教程。 MoveMe來自蘋果的樣品是一個很好的例子。
+0
謝謝你的幫助。 – jdl 2011-05-24 20:38:35
2
func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) {
print("tap working")
if gestureRecognizer.state == UIGestureRecognizerState.Recognized
{
`print(gestureRecognizer.locationInView(gestureRecognizer.view))`
}
}
0
斯威夫特3回答
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapAction(_:)))
yourView.addGestureRecognizer(tapGesture)
func tapAction(_ sender: UITapGestureRecognizer) {
let point = sender.location(in: yourView)
}
相關問題
- 1. 如何在UIViewController中獲取UIView的框架座標?
- 2. 獲取手指觸摸UIView的次數
- 3. 的UIButton接受1個手指2的手指敲擊事件
- 4. 在UIView級別獲取UITextField Y座標?
- 5. 如何根據鼠標點擊獲取3D中點的座標?
- 6. 如何在縮放後獲得正確的座標?用手指
- 7. 獲取鼠標屏幕座標點擊
- 8. 如何獲取鼠標點擊時的座標
- 9. 如何獲取vtkboxwidget座標
- 10. 如何獲取UIImageView座標?
- 11. 的UIScrollView和敲擊手勢
- 12. 如何獲取InkCanvas中的手指數?
- 13. 如何在Xamarin.Forms中點擊地圖時獲取座標?
- 14. 如何顯示圖片並獲取鼠標點擊座標
- 15. 點擊圖片,獲取座標
- 16. 如何獲取面板內點擊事件的座標?
- 17. 如何獲取按鈕點擊div的座標
- 18. 如何獲取鼠標座標和.push()?
- 19. 從框架中獲取鼠標單擊的座標
- 20. 在C#中單擊鼠標獲取鼠標座標
- 21. 如何獲取angularJs指令鏈接函數中元素標籤的座標
- 22. 如何獲取Gridlayout的座標
- 23. 如何獲取圖像的座標/ JLabel?
- 24. 如何獲取mxCell的座標?
- 25. 如何從locationManager獲取viewDidLoad的座標?
- 26. 如何獲取動畫UIImageView的座標
- 27. 如何獲取RecycleView的滾動座標?
- 28. 如何獲取GtkTreeView行的座標
- 29. 如何獲取UIImage的地理座標?
- 30. 如何獲取瀏覽器的座標?
touchesEnded:withEvent:也可以在UIViewController中使用,因爲這也是從UIResponder派生的。 – taskinoor 2011-05-24 16:54:35
謝謝你們......這給了一個好的開始! – jdl 2011-05-24 20:36:13