防止在 MKMapView 中滚动,也在缩放时 [英] Prevent scrolling in a MKMapView, also when zooming
问题描述
一旦用户开始捏住 MKMapView,scrollEnabled
似乎会被破坏.
The scrollEnabled
seems to be breakable once the user starts pinching in a MKMapView.
您仍然无法用一根手指滚动,但如果在放大和缩小时用两根手指滚动,您可以移动地图.
You still can't scroll with one finger, but if you scroll with two fingers while zooming in and out, you can move the map.
我试过了:
- 继承
MKMapKit
以禁用其中的滚动视图. - 实施 –
mapView:regionWillChangeAnimated:
以强制居中. - 禁用
scrollEnabled
.
- Subclassing the
MKMapKit
to disable the scroll view inside it. - Implementing –
mapView:regionWillChangeAnimated:
to enforce the center. - Disabling
scrollEnabled
.
但没有运气.
谁能告诉我只放大 MKMapView 的可靠方法,这样中心点始终保持在中间?
Can anyone tell me a sure way to ONLY have zooming in a MKMapView, so the center point always stays in the middle ?
推荐答案
您可以尝试使用 UIPinchGestureRecognizer
自己处理捏合手势:
You can try to handle the pinch gestures yourself using a UIPinchGestureRecognizer
:
首先将 scrollEnabled
和 zoomEnabled
设置为 NO
并创建手势识别器:
First set scrollEnabled
and zoomEnabled
to NO
and create the gesture recognizer:
UIPinchGestureRecognizer* recognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePinch:)];
[self.mapView addGestureRecognizer:recognizer];
在识别器处理程序中根据缩放比例调整MKCoordinateSpan
:
In the recognizer handler adjust the MKCoordinateSpan
according to the zoom scale:
- (void)handlePinch:(UIPinchGestureRecognizer*)recognizer
{
static MKCoordinateRegion originalRegion;
if (recognizer.state == UIGestureRecognizerStateBegan) {
originalRegion = self.mapView.region;
}
double latdelta = originalRegion.span.latitudeDelta / recognizer.scale;
double londelta = originalRegion.span.longitudeDelta / recognizer.scale;
// TODO: set these constants to appropriate values to set max/min zoomscale
latdelta = MAX(MIN(latdelta, 80), 0.02);
londelta = MAX(MIN(londelta, 80), 0.02);
MKCoordinateSpan span = MKCoordinateSpanMake(latdelta, londelta);
[self.mapView setRegion:MKCoordinateRegionMake(originalRegion.center, span) animated:YES];
}
这可能不像 Apple 的实现那样完美,但它应该可以解决您的问题.
This may not work perfectly like Apple's implementation but it should solve your issue.
这篇关于防止在 MKMapView 中滚动,也在缩放时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!