捏MKMapView时保持中心坐标

如果您在追踪设备的位置时捏紧放大/缩小Apple的地图应用程序,则捏合手势的“平移”组件将被忽略,蓝色位置指示器将保持固定在屏幕中央。 当使用普通的MKMapView时,情况并非如此。

假设我已经有了用户的位置,我怎么能达到这个效果呢? 我已经尝试在regionDid/WillChangeAnimated:regionDid/WillChangeAnimated:方法中重置中心坐标,但是只在手势的开始和结束时调用它们。 我也尝试添加一个UIPinchGestureRecognizer子类,当触摸移动时重置中心坐标,但是这导致了毛刺。


编辑:对于有兴趣的人,下面的作品对我来说。

 // CenterGestureRecognizer.h @interface CenterGestureRecognizer : UIPinchGestureRecognizer - (id)initWithMapView:(MKMapView *)mapView; @end 

 // CenterGestureRecognizer.m @interface CenterGestureRecognizer () - (void)handlePinchGesture; @property (nonatomic, assign) MKMapView *mapView; @end @implementation CenterGestureRecognizer - (id)initWithMapView:(MKMapView *)mapView { if (mapView == nil) { [NSException raise:NSInvalidArgumentException format:@"mapView cannot be nil."]; } if ((self = [super initWithTarget:self action:@selector(handlePinchGesture)])) { self.mapView = mapView; } return self; } - (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer { return NO; } - (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer { return NO; } - (void)handlePinchGesture { CLLocation *location = self.mapView.userLocation.location; if (location != nil) { [self.mapView setCenterCoordinate:location.coordinate]; } } @synthesize mapView; @end 

然后简单地把它添加到你的MKMapView

 [self.mapView addGestureRecognizer:[[[CenterGestureRecognizer alloc] initWithMapView:self.mapView] autorelease]]; 

当用户捏住实际设备上的屏幕(而不是模拟器)时,它会同时引起平移捏合手势 – 捏合包含运动的“缩放”元素,而平移包含垂直和水平变化。 你需要拦截和阻止锅,这意味着使用UIPanGestureRecognizer

scrollEnabled设置为NO ,然后添加一个UIPanGestureRecognizer来重置中心坐标。 该组合将阻止双指平移和掐指的平底锅组分。


编辑后添加更多的细节,看到你的代码之后: touchesMoved:withEvent在pan已经开始之后调用touchesMoved:withEvent ,所以如果你在那里改变MKMapView的中心,你会得到你所描述的生气的渲染问题。 你真正需要的是创build一个带有目标动作的UIPanGestureRecognizer ,如下所示:

  UIPanGestureRecognizer *pan = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizePan)] autorelease]; pan.delegate = self; [self.mapView addGestureRecognizer:pan]; 

…然后添加一个didRecognizePan方法到您的控制器,并做你的中心重置在那里。

只是一个猜测,但你有没有尝试在regionWillChangeAnimated:的开始设置scrollEnabledNO regionWillChangeAnimated:

只是一个猜测。 在regionWillChangeAnimated的开始处:保存当前的地图区域,然后使用self.myMapView.region = theSavedRegion或类似的方法通过NSTimer持续更新区域。 然后在调用regionDidChangeAnimated:时使计时器无效。

但是,您可能会遇到由NSTimer更新区域导致regionWillChangeAnimated再次被调用的问题。

试一试,看看会发生什么。