使用UIScreenEdgePanGestureRecognizer而不移动MKMapView
我有一个包含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;
但是随着抽屉的出现,地图正在移动! 我试过所有的技巧,以防止它,但无法…(我试过shouldBeRequiredToFailByGestureRecognizer
或requireGestureRecognizerToFail
例如)
任何想法如何我可以防止MapView的移动,当手势是从LeftEdge ScreenEdgePan?
我在我的应用程序中做了以下几件事:
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; }
并启用/禁用像这样的滚动的地图视图
- (void)handlePopRecognizer:(UIScreenEdgePanGestureRecognizer*)recognizer { if(recognizer.state == UIGestureRecognizerStateBegan){ _mapView.scrollEnabled = NO; } else if(recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled){ _mapView.scrollEnabled = YES; } }
希望能帮助到你。
UIScreenEdgePanGestureRecognizer
开始识别时,地图上的平移手势需要立即取消。 为了达到这个目的,将scrollEnabled
设置为NO
。 这将取消另一个手势识别器。
- (void) handleEdgeGesture:(UIScreenEdgePanGestureRecognizer*)recognizer { if(recognizer.state == UIGestureRecognizerStateBegan) { // cancel simultaneous gesture on map view _mapView.scrollEnabled = NO; _mapView.scrollEnabled = YES; } }
- (void) handleEdgeGesture:(UIScreenEdgePanGestureRecognizer*)recognizer : (id)sender { if(recognizer.state == UIGestureRecognizerStateBegan && sender == GMSMapView) { // cancel simultaneous gesture on map view _mapView.isUserInteractionEnabled = NO; } }
我相信最快的解决scheme是在mapView的顶部制作一个薄的视图(透明的背景色),在那里你的手势不应该在mapView上工作。 即
下面的组合为我工作,而不触及地图视图。
// 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; }