我有一个带有单个子视图的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 :(得分: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
}
}
}