簡體   English   中英

MKMapView和響應者鏈

[英]MKMapView and the responder chain

我有一個帶有單個子視圖的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可以達到我在這里期望的效果,但是我不能使用該方法,因為在子視圖中我需要響應其他手勢。

如何使用添加到mapView的UIGestureRecognizers處理所有手勢(正確設置為忽略其他手勢識別器或與它們同時觸發)並禁用子視圖的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
        }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM