我正在嘗試在我的MKMapView
上捕獲點按活動,這樣我可以在用戶點擊的位置放置MKPinAnnotation
。基本上我有一張覆蓋MKOverlayViews
(覆蓋顯示建築物)的地圖,我想通過刪除MKPinAnnotaion
並在標註中顯示更多信息,爲用戶提供關於該覆蓋圖的更多信息。 謝謝。如何在MKMapView上捕獲點按手勢
30
A
回答
59
您可以使用UIGestureRecognizer
來檢測地圖視圖上的觸摸。但是,我建議您不要單擊一次(UITapGestureRecognizer
)或長按(UILongPressGestureRecognizer
)。一次點擊可能會干擾試圖單擊該引腳或標註本身的用戶。
在地方,你設置地圖視圖(在viewDidLoad
例如),手勢識別附加到地圖視圖:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleGesture:)];
tgr.numberOfTapsRequired = 2;
tgr.numberOfTouchesRequired = 1;
[mapView addGestureRecognizer:tgr];
[tgr release];
或使用長按:
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleGesture:)];
lpgr.minimumPressDuration = 2.0; //user must press for 2 seconds
[mapView addGestureRecognizer:lpgr];
[lpgr release];
在handleGesture:
方法:
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate =
[mapView convertPoint:touchPoint toCoordinateFromView:mapView];
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = touchMapCoordinate;
pa.title = @"Hello";
[mapView addAnnotation:pa];
[pa release];
}
5
我在viewDidLoad:
中設置了一個長按(UILongPressGestureRecognizer
),但它只檢測到第一個觸摸。
我在哪裏可以設置長按來檢測所有觸摸?(這意味着地圖準備就緒,每次等待用戶觸摸到屏幕推一個針)
該方法viewDidLoad:
!
- (void)viewDidLoad {
[super viewDidLoad];mapView.mapType = MKMapTypeStandard;
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
[self.mapView addGestureRecognizer:longPressGesture];
[longPressGesture release];
mapAnnotations = [[NSMutableArray alloc] init];
MyLocation *location = [[MyLocation alloc] init];
[mapAnnotations addObject:location];
[self gotoLocation];
[self.mapView addAnnotations:self.mapAnnotations];
}
和handleLongPressGesture
方法:
-(void)handleLongPressGesture:(UIGestureRecognizer*)sender {
// This is important if you only want to receive one tap and hold event
if (sender.state == UIGestureRecognizerStateEnded)
{NSLog(@"Released!");
[self.mapView removeGestureRecognizer:sender];
}
else
{
// Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
CGPoint point = [sender locationInView:self.mapView];
CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
// Then all you have to do is create the annotation and add it to the map
MyLocation *dropPin = [[MyLocation alloc] init];
dropPin.latitude = [NSNumber numberWithDouble:locCoord.latitude];
dropPin.longitude = [NSNumber numberWithDouble:locCoord.longitude];
// [self.mapView addAnnotation:dropPin];
[mapAnnotations addObject:dropPin];
[dropPin release];
NSLog(@"Hold!!");
NSLog(@"Count: %d", [mapAnnotations count]);
}
}
1
如果你想在地圖視圖使用一個單一的點擊/點按,這裏的代碼片段我使用。 (可可和斯威夫特)
let gr = NSClickGestureRecognizer(target: self, action: "createPoint:")
gr.numberOfClicksRequired = 1
gr.delaysPrimaryMouseButtonEvents = false // allows +/- button press
gr.delegate = self
map.addGestureRecognizer(gr)
在手勢委託方法
,一個簡單的測試,以比較喜歡雙擊手勢...
func gestureRecognizer(gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: NSGestureRecognizer) -> Bool {
let other = otherGestureRecognizer as? NSClickGestureRecognizer
if (other?.numberOfClicksRequired > 1) {
return true; // allows double click
}
return false
}
你還可以過濾手勢在其他委託方法,如果你想要的地圖在各種「州」,其中一個允許單擊/點擊
相關問題
- 1. 在地圖上點按手勢
- 2. MKMapView上的手勢識別 - iPhone
- 3. 通過UIWebView捕獲手勢
- 4. 如何在UIWebView上的長按手勢創建點擊事件?
- 5. iOS按住手勢+點擊
- 6. 捕獲結合按下按鈕再移動手勢
- 7. Android:手勢檢測器不會捕獲手勢
- 8. 如何手動取消MKMapView標準手勢識別器
- 9. 如何在iOS中的UItableCell中的按鈕上添加點按手勢?
- 10. UIImageView上的長按手勢
- 11. 取消在iPhone的UIBarbutton點按手勢
- 12. 使用UIGestureRecognizer在MKOverlayView上捕獲手勢事件
- 13. WinJS gestureRecognizer - 如何捕捉多個手勢
- 14. 在mkmapview上檢測不是註釋的輕擊手勢?
- 15. 捕捉全局手勢(如何構建AppStore「購買」按鈕)
- 16. 如何在Xamarin Forms中長按手勢?
- 17. 如何在視圖動畫時識別點按手勢
- 18. 許多點擊手勢在視圖上
- 19. 如何捕獲pageControl內部的tableView的橫向滑動手勢?
- 20. 誘捕UIView中的手勢
- 21. 如何在MKMapView上添加按鈕?
- 22. UIImage不會識別點按手勢Swift
- 23. 點按手勢不起作用
- 24. LibGdx:如何在Actor上實現手勢
- 25. 如何在iPhone上捏手勢?
- 26. iOS7 Sprite Kit如何在SKSpriteNode上長按或其他手勢?
- 27. 如何在iOS的UIWebView上禁用長按手勢
- 28. 如何在iPad上同時允許多個長按手勢?
- 29. 如何在android上實現一個按鈕的手勢?
- 30. 捕獲MKMapView屏幕截圖
謝謝你的建議,一旦我有它的工作,我會盡快回復。我確實嘗試過單擊,但之後我無法顯示我的PinAnnotations的標註。看起來我需要使用LongPressureGesture – sbkb 2010-12-02 16:09:46