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

如何处理UIGestureRecognizers添加到 mapView 并禁用子视图的所有手势(正确设置为忽略其他手势识别器或同时触发它们)userInteractionEnabled

我使用以下代码在不干扰标准手势的情况下收听 mapView 上的 Taps:

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 回答