-
Notifications
You must be signed in to change notification settings - Fork 122
Adds a delegate to notify when a gesture is canceled. #284
base: main
Are you sure you want to change the base?
Conversation
3a2949a
to
7a247a0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add this delegate method if necessary, but did we try out the approach suggested in mapbox/mapbox-gl-native#14686 (comment)? It seems pretty elegant:
Probably the best place to edit it would be method shouldChangeFromCamera:toCamera:reason:.
You could be passing newCamera as a reference - so it'd be editable for those who want it, or not, for those who don't want it. Or make another delegate method to eventually edit it - if you want to keep in sync with the old version.
We can’t pass in newCamera
by reference without changing the signature in a backwards-compatible change. However, the camera is mutable, so after calling -_shouldChangeFromCamera:toCamera:
, we could check if any of newCamera
’s properties have changed; if they have, we could bail out of the usual gesture handling and set that camera instead (animated or unanimated depending on the call site).
@fabian-guerra any reason why we couldn't try this first? |
@1ec5 @julianrex I didn't consider following their suggestion because:
I would encourage you to try the example to see how it works. |
What gives me pause about the proposed addition to the API is that it tells the delegate that the map view “did” cancel the gesture when in fact that hasn’t happened and wouldn’t even happen with an empty implementation of the method. At most, this method is one of a few opportunities for the delegate to cancel the gesture. The naming of this method is critical, because it affects when developers expect it to be called in the order of camera-related delegate methods.
mapbox/mapbox-gl-native#2457 (comment) proposed following the precedent in Since we’re dealing with an MGLMapCamera object, we don’t need to make it an inout object, which means the method signature would stay the same. There is a semantic difference between a “should” method and a “will” method. But currently only the “should” method tells the application what the new camera will be.
My naïve assumption was that a developer could implement The example being added to iosapp seems a bit contrived, but it does effectively demonstrate your point that the existing “should” methods aren’t well-suited for the use case in mapbox/mapbox-gl-native#14686. I wonder if it would be feasible to blend your approach with the UIScrollViewDelegate precedent: |
if ([self.delegate respondsToSelector:@selector(mapView:didCancelGestureForCamera:)]) { | ||
[self.delegate mapView:self didCancelGestureForCamera:toCamera]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ([self.delegate respondsToSelector:@selector(mapView:didCancelGestureForCamera:)]) { | |
[self.delegate mapView:self didCancelGestureForCamera:toCamera]; | |
} | |
MGLMapCamera *targetCamera = toCamera.copy; | |
if ([self.delegate respondsToSelector:@selector(mapViewWillEndPanning:targetCamera:)]) { | |
[self.delegate mapViewWillEndPanning:self targetCamera:targetCamera]; | |
if (![toCamera isEqualToCamera:targetCamera]) { | |
[self setCamera:targetCamera animated:YES]; | |
} | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, that would have to happen regardless of -mapView:shouldChangeFromCamera:toCamera:
, so we’d move it out one level so it’s only conditionalized on drift
.
As the PR stands, I think a developer would want to know the type of gesture that was beginning to drift. Also I'm not sure that Another option (!) might be to add a sibling to However I like @1ec5's explicit |
Yes, the idea with |
I need to get my way to working with my iPhone |
Fixes mapbox/mapbox-gl-native#14686. Added a delegate to gracefully manipulate the camera once a gesture is canceled programatically.