我正在研究iPad的圖形計算器應用程序,我想添加一個功能,用戶可以在圖形視圖中點擊一個區域,使文本框彈出並顯示它們所觸摸的點的座標。我怎樣才能從這個CGPoint?如何從抽頭位置獲取CGPoint?
26
A
回答
46
你有兩種方式...
1.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
}
在這裏,你可以得到從當前視點位置...
2.
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[self.view addGestureRecognizer:tapRecognizer];
這裏,這個代碼使用時,你想與你的主視圖或你的主視圖的子視圖做什麼
19
嘗試這個
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
// Get the specific point that was touched
CGPoint point = [touch locationInView:self.view];
NSLog(@"X location: %f", point.x);
NSLog(@"Y Location: %f",point.y);
}
您可以使用「touchesEnded」如果你寧願看到用戶解除他們的手指離開屏幕,而不是在那裏降落。
3
如果您使用的是UIGestureRecognizer
或UITouch
對象,則可以使用locationInView:
方法檢索用戶觸摸的給定視圖中的CGPoint
。
6
將UIGestureRecognizer與地圖視圖一起使用可能會更好更簡單,而不是試圖對其進行子類化並手動攔截觸摸。
步驟1:首先,手勢識別添加到地圖視圖:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(tapGestureHandler:)];
tgr.delegate = self; //also add <UIGestureRecognizerDelegate> to @interface
[mapView addGestureRecognizer:tgr];
第2步:接下來,實施shouldRecognizeSimultaneouslyWithGestureRecognizer並返回YES,以便您的點觸手勢識別器可以同時作爲地圖的工作(否則輕敲引腳將無法獲得通過地圖自動處理):
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer
:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
步驟3:最後,實現手勢處理機:
- (void)tapGestureHandler:(UITapGestureRecognizer *)tgr
{
CGPoint touchPoint = [tgr locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate
= [mapView convertPoint:touchPoint toCoordinateFromView:mapView];
NSLog(@"tapGestureHandler: touchMapCoordinate = %f,%f",
touchMapCoordinate.latitude, touchMapCoordinate.longitude);
}
0
func handleFrontTap(gestureRecognizer: UITapGestureRecognizer) {
print("tap working")
if gestureRecognizer.state == UIGestureRecognizerState.Recognized {
`print(gestureRecognizer.locationInView(gestureRecognizer.view))`
}
}
3
只想在斯威夫特4答案折騰,因爲API是完全不同的期待。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = event?.allTouches?.first {
let loc:CGPoint = touch.location(in: touch.view)
//insert your touch based code here
}
}
OR
let tapGR = UITapGestureRecognizer(target: self, action: #selector(tapped))
view.addGestureRecognizer(tapGR)
@objc func tapped(gr:UITapGestureRecognizer) {
let loc:CGPoint = gr.location(in: gr.view)
//insert your touch based code here
}
在這兩種情況下loc
將包含在視圖觸摸的點。
相關問題
- 1. 如何獲取viewAtLocation:(CGPoint)的位置?
- 2. 如何從UIScrollView中獲取抽頭位置是覆蓋的UITextView
- 3. 如何從Reponse頭獲取位置?
- 4. 如何使用谷歌地圖sdk獲取CGPoint的iOS位置?
- 5. 問:如何從NSConcreteValue獲取CGPoint?
- 6. 圓CGPoint位置
- 7. 如何從StackPanel中的抽頭事件中獲取抽頭項目
- 8. 從響應頭獲取位置
- 9. UITapGestureRecognizer抽頭位置困境
- 10. 如何獲取UIButton中的抽頭數?
- 11. 位置不同CGPoint
- 12. 如何從jQuery獲取響應頭位置?
- 13. 從位置獲取位置
- 14. 如何從CGPoint
- 15. 如何從抽頭單詞中獲取字符範圍UITextview
- 16. CGPoint位置= [touch locationInView:[touch view]];
- 17. 如何從GridView獲取位置
- 18. 如何從指定位置獲取NSView
- 19. 如何從getCurrentPosition獲取位置?
- 20. 如何從服務中獲取位置
- 21. 如何從Backstack位置獲取片段
- 22. 如何從GridView獲取滾動位置?
- 23. 如何從webelement獲取Xpath位置
- 24. 如何從MouseDown獲取全球位置?
- 25. 如何從onCreateViewHolder獲取位置
- 26. 如何從onChildClick()獲取組位置?
- 27. 的Android定製的雙抽頭位置
- 28. jQuery顯示標記在抽頭位置
- 29. 在用戶抽頭位置繪製UIImageView
- 30. 使用GDataXML從XML獲取CGPoint?
這是一個很好的完整答案 –