2009-08-14 144 views

回答

4
+0

這幫助,但問題。但是總比沒有好...... – bentford 2009-11-13 08:51:20

+1

是的,如果你在評論中看到另一個解決方案。實際上還有第三種方法(除了對MKMapView進行子分類或將自己的視圖分層放置並傳遞給MKMapView)。不是繼承MKMapView,而是繼承主窗口的子類,並將觸發事件傳遞給MKMapView和您的視圖。 http://stackoverflow.com/questions/1121889/intercepting-hijacking-iphone-touch-events-for-mkmapview/1298330還沒有嘗試過,但顯然性能更好,捏縮放仍然有效。 – ChrisJF 2010-10-22 13:34:54

+0

此鏈接不再有效 – 2013-06-04 11:43:35

1

你在這個時候攔截觸摸在地圖視圖不能,你可以嘗試分層有一個不透明的視圖,看看它是否拿起觸摸...

2

或者取決於你想要做什麼,添加一個MKAnnotation(圖釘,帶有標註),所以你有一些東西可以挖掘 - 然後你的地圖代表會收到一個事件,例如。

mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

30

如果你只是希望獲得通知輕敲姿勢,而不會影響任何地圖上的其他觸摸的行爲,你會想用UITapGestureRecognizer。這是非常簡單的,只是放入這樣的代碼。

UITapGestureRecognizer* tapRec = [[UITapGestureRecognizer alloc] 
    initWithTarget:self action:@selector(didTapMap:)]; 
[theMKMapView addGestureRecognizer:tapRec]; 
[tapRec release]; 

,將調用每當theMKMapView接收敲擊手勢,所有的捏和拖動手勢仍然可以工作,因爲他們沒有以前didTapMap

+0

對我來說,設置一個選擇器不起作用,但在使用委託功能時起作用(gestureRecognizerShouldBegin :) 順便說一句,儘管Tap和Pan工作,UIPinchGestureRecognizer不起作用,甚至不使用委託函數。 – 2012-05-23 15:04:19

+0

我不知道該怎麼告訴你,沒有比這更具體的細節...使用選擇器總是適合我。 – pseudopeach 2012-05-24 20:41:15

+1

冒號缺失,ir應爲'@selector(didTapMap :) :) – Ali 2012-06-25 00:03:22

2

在iOS 8

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

     UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil]; 
     doubleTap.numberOfTapsRequired = 2; 
     doubleTap.numberOfTouchesRequired = 1; 
     [self.mapView addGestureRecognizer:doubleTap]; 

     UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; 
     singleTap.numberOfTapsRequired = 1; 
     singleTap.numberOfTouchesRequired = 1; 
     [singleTap requireGestureRecognizerToFail: doubleTap]; 
     [self.mapView addGestureRecognizer:singleTap]; 
    } 

    - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer 
    { 
      if (gestureRecognizer.state != UIGestureRecognizerStateEnded) 
       return; 
      //Do your work ... 
    } 
0

完美的工作只是添加一些代碼段爲@ TT-kilew答案的例證。就我而言,我想在地圖上將用戶指向自己,但不想打斷他的拖動觸摸。

@interface PrettyViewController() <MKMapViewDelegate> 

@property (weak, nonatomic) IBOutlet MKMapView *mapView; 
@property (assign, nonatomic) BOOL userTouchTheMap; 

@end 

@implementation PrettyViewController 

#pragma mark - UIResponder 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 
    [super touchesBegan:touches withEvent:event]; 

    self.userTouchTheMap = [[touches anyObject].view isEqual:self.mapView]; 
} 


#pragma mark - MKMapViewDelegate 

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { 
    //We just positioning to user 
    if (!self.userTouchTheMap) { 
     CLLocationDistance radius = 5000; 
     [self.mapView setRegion:MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 2*radius, 2*radius) animated:YES]; 
    } 
} 

@end 
0

沒有我發現過工作,但我想出了這個不完美的解決方案: 在viewDidLoad中

let singleTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(onMapClicked)) 
singleTapRecognizer.delegate = self 
mapView.addGestureRecognizer(singleTapRecognizer) 

在委託:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { 
    return touch.view!.frame.equalTo(mapView.frame) 
}