I have a UIViewController containing a MKMapView (in fact, it contains a full screen container containing the MKMapView, but it shouldn't have any impact)
I implemented a UIScreenEdgePanGestureRecognizer (to show a drawer) like this:
self.swipeRight = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleEdgeGesture:)];
[self.swipeRight setEdges:UIRectEdgeLeft];
[self.swipeRight setDelegate:self];
[self.view addGestureRecognizer:self.swipeRight];
and to make it works I had to add the following method (returning YES):
(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
But then the map is moving at the same time as the drawer is appearing!
I've tried all kind of tricks to prevent it but was not able to... (I tried shouldBeRequiredToFailByGestureRecognizer
or requireGestureRecognizerToFail
for example)
Any idea how I could prevent the MapView from moving when the gesture is a ScreenEdgePan from LeftEdge?
What I did in my app was the following:
UIScreenEdgePanGestureRecognizer *popRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePopRecognizer:)];
popRecognizer.edges = UIRectEdgeLeft;
popRecognizer.delegate = self;
Then set the delegate to YES as you said
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
And enable/disable scrolling of the mapview like this
- (void)handlePopRecognizer:(UIScreenEdgePanGestureRecognizer*)recognizer
{
if(recognizer.state == UIGestureRecognizerStateBegan){
_mapView.scrollEnabled = NO;
} else if(recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled){
_mapView.scrollEnabled = YES;
}
}
Hope it helps.
The pan gesture on the map needs to be cancelled as soon as the UIScreenEdgePanGestureRecognizer
begins to recognize. To achieve this it is sufficient to set scrollEnabled
to NO
momentarily. This will cancel the other gesture recognizer.
- (void) handleEdgeGesture:(UIScreenEdgePanGestureRecognizer*)recognizer
{
if(recognizer.state == UIGestureRecognizerStateBegan) {
// cancel simultaneous gesture on map view
_mapView.scrollEnabled = NO;
_mapView.scrollEnabled = YES;
}
}
The following combination worked for me without touching the map view.
// 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With