在捏取MKMapView时保持中心坐标

Chr*_*ble 10 iphone objective-c mkmapview ios

如果您在跟踪设备的位置时捏合放大/缩小Apple的地图应用程序,则会忽略捏合手势的"平移"组件,蓝色位置指示器将保持固定在屏幕中央.使用普通纸时不是这种情况MKMapView.

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


编辑:对于有兴趣的人,以下适用于我.

// CenterGestureRecognizer.h
@interface CenterGestureRecognizer : UIPinchGestureRecognizer

- (id)initWithMapView:(MKMapView *)mapView;

@end
Run Code Online (Sandbox Code Playgroud)

// 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
Run Code Online (Sandbox Code Playgroud)

然后只需将其添加到您的MKMapView:

[self.mapView addGestureRecognizer:[[[CenterGestureRecognizer alloc] initWithMapView:self.mapView] autorelease]];
Run Code Online (Sandbox Code Playgroud)

Sco*_*bes 5

当用户在实际设备上(而不是模拟器)捏住屏幕时,它会导致平移捏合手势 - 捏合包含动作的"缩放"元素,并且平移包含垂直和水平变化.你需要拦截和阻止平底锅,这意味着使用UIPanGestureRecognizer.

设置scrollEnabledNO,然后添加一个UIPanGestureRecognizer以重置中心坐标.该组合将阻止单指平移和捏合的平移组件.


编辑以添加更多详细信息,并在看到您的代码touchesMoved:withEvent之后:在pan已经开始之后调用,因此如果您在那里更改MKMapView的中心,您将获得您所描述的笨拙的渲染问题.你真正需要的是创建UIPanGestureRecognizer一个目标动作,如下所示:

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

...然后didRecognizePan向控制器添加一个方法,并在那里进行中心重置.