0

単一のサブビューを持つ 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 をオーバーライドすると、ここで期待していることを実現できますが、サブビューで応答する必要がある他のジェスチャーがあるため、そのアプローチを使用できません。

4

1 に答える 1

0

UIGestureRecognizersmapView に追加されたすべてのジェスチャ(他のジェスチャ認識エンジンを無視するか、同時に起動するように適切に設定) を使用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
        }
}

}

于 2012-08-06T01:22:38.387 に答える