2012-08-05 31 views
0

我有一個的MKMapView與單個子視圖:的MKMapView和響應鏈

MKMapView *mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, 200, 200)]; 
subView.backgroundColor = [UIColor grayColor]; 
[mapView addSubview:subView]; 

我預計,由於子視圖不處理任何觸摸事件,所有觸摸事件,將沿父地圖視圖通過(通過響應者鏈)。然後,我會期望在子視圖中平移和捏合會平移和捏住地圖。

不幸的是,這似乎並不是這樣。有誰知道一種將地圖視圖導入響應者鏈的方法嗎?

我意識到覆蓋hitTest在我的子視圖可以實現我在這裏期待的,但我不能使用這種方法,因爲我有其他手勢,我需要在子視圖中做出響應。

回答

0

如何處理所有手勢UIGestureRecognizers(正確設置忽略其他手勢識別器或與他們同時開火)添加到您的mapView和禁用userInteractionEnabled你的子視圖?

我用下面的代碼來聽取的MapView水龍頭,而不符合標準的手勢干擾:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mtd_handleMapTap:)]; 

// we require all gesture recognizer except other single-tap gesture recognizers to fail 
for (UIGestureRecognizer *gesture in self.gestureRecognizers) { 
    if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) { 
     UITapGestureRecognizer *systemTap = (UITapGestureRecognizer *)gesture; 

     if (systemTap.numberOfTapsRequired > 1) { 
      [tap requireGestureRecognizerToFail:systemTap]; 
     } 
    } else { 
     [tap requireGestureRecognizerToFail:gesture]; 
    } 
} 


- (void)mtd_handleMapTap:(UITapGestureRecognizer *)tap { 
if ((tap.state & UIGestureRecognizerStateRecognized) == UIGestureRecognizerStateRecognized) { 

     // Get view frame rect in the mapView's coordinate system 
     CGRect viewFrameInMapView = [self.mySubview.superview convertRect:self.mySubview.frame toView:self.mapView]; 
     // Get touch point in the mapView's coordinate system 
     CGPoint point = [tap locationInView:self.mapView]; 

     // Check if the touch is within the view bounds 
     if (CGRectContainsPoint(viewFrameInMapView, point)) { 
      // tap was on mySubview 
     } 
} 

}

+0

我需要我的子視圖手勢來僅在子視圖執行時操作。我相信如果我採用這種方法,他們可以在整個mapview中使用。 – mark 2012-08-06 04:08:01

+0

我在上面編輯了我的答案,您可以嘗試檢查手勢是否在子視圖的框架內 – myell0w 2012-08-06 17:19:43