如果捏在苹果的地图应用程序中放大/缩小,跟踪设备的位置,捏手势的“平移”组件将被忽略,蓝色位置指示器保持固定在屏幕。当使用普通的MKMapView
时,情况并非如此。保持中心坐标,同时捏MKMapView
假设我已经有用户的位置,我怎么能达到这个效果?我尝试重置代表的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'子类的实现:http://pastie.org/1934011 – 2011-05-20 23:35:54
这就实现了!我很高兴解决方案非常简单。谢谢,斯科特。 – 2011-05-21 01:59:53