使用UIScreenEdgePanGestureRecognizer而不移动MKMapView

时间:2014-07-18 20:41:56

标签: ios objective-c mkmapview uigesturerecognizer

我有一个包含MKMapView的UIViewController(事实上,它包含一个包含MKMapView的全屏容器,但它不应该有任何影响)

我实现了一个UIScreenEdgePanGestureRecognizer(显示抽屉):

self.swipeRight = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleEdgeGesture:)];
[self.swipeRight setEdges:UIRectEdgeLeft];
[self.swipeRight setDelegate:self];
[self.view addGestureRecognizer:self.swipeRight];

并使其工作我必须添加以下方法(返回YES):

(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

然后地图在抽屉出现的同时移动! 我尝试过各种各样的技巧来阻止它但却无法......(我试过shouldBeRequiredToFailByGestureRecognizerrequireGestureRecognizerToFail

当手势是来自LeftEdge的ScreenEdgePan时,我知道如何防止MapView移动?

5 个答案:

答案 0 :(得分:16)

我在我的应用中所做的是以下内容:

UIScreenEdgePanGestureRecognizer *popRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePopRecognizer:)];
popRecognizer.edges = UIRectEdgeLeft;
popRecognizer.delegate = self;

然后按照说明

将委托设置为YES
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

启用/禁用像这样滚动mapview

- (void)handlePopRecognizer:(UIScreenEdgePanGestureRecognizer*)recognizer
{
    if(recognizer.state == UIGestureRecognizerStateBegan){
        _mapView.scrollEnabled = NO;
    } else if(recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled){
        _mapView.scrollEnabled = YES;

    }
}

希望它有所帮助。

答案 1 :(得分:3)

UIScreenEdgePanGestureRecognizer开始识别时,地图上的平移手势需要取消。要实现这一点,暂时将scrollEnabled设置为NO就足够了。这将取消其他手势识别器。

- (void) handleEdgeGesture:(UIScreenEdgePanGestureRecognizer*)recognizer
{
    if(recognizer.state == UIGestureRecognizerStateBegan) {
        // cancel simultaneous gesture on map view
        _mapView.scrollEnabled = NO;
        _mapView.scrollEnabled = YES;
    }
}

答案 2 :(得分:2)

以下组合适用于我而不触及地图视图。

// This is to ensure UIScreenEdgePanGestureRecognizer won't be blocked by other gestures. 
// You may need to do some logic checking before returning YES.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
  return YES;
}
// This is to prevent other recognisers when UIScreenEdgePanGestureRecognizer
// is recognising the gesture. Again, you may want to do some logic checking
// before returning to YES.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

  return YES;
}

答案 3 :(得分:1)

- (void) handleEdgeGesture:(UIScreenEdgePanGestureRecognizer*)recognizer : (id)sender
{
    if(recognizer.state == UIGestureRecognizerStateBegan && sender == GMSMapView) {
        // cancel simultaneous gesture on map view
        _mapView.isUserInteractionEnabled = NO;
    }
}

答案 4 :(得分:1)

我认为最快的解决方案是在mapView上制作一个薄视图(透明背景颜色),你的手势不应该在mapView上工作。即 enter image description here