如果您在跟踪设备位置时在 Apple 地图应用程序中进行捏合放大/缩小,则捏合手势的“平移”部分将被忽略,蓝色位置指示器将保持固定在屏幕中央。使用普通的时情况并非如此MKMapView
.
假设我已经有了用户的位置,我怎样才能达到这个效果呢?我尝试重置代表的中心坐标regionDid/WillChangeAnimated:
方法,但它们仅在手势开始和结束时调用。我也尝试添加UIPinchGestureRecognizer
当触摸移动时重置中心坐标的子类,但这会导致渲染故障。
Edit:对于那些感兴趣的人,以下内容对我有用。
// 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]];
当用户在实际设备(而不是模拟器)上捏合屏幕时,会导致平移and捏合手势 – 捏合包含运动的“缩放”元素,平移包含垂直和水平变化。你需要拦截并阻挡平底锅,这意味着使用UIPanGestureRecognizer
.
Set scrollEnabled
to NO
,然后添加一个UIPanGestureRecognizer
重置中心坐标。该组合将阻止单指平移和捏合的平移部分。
Edit添加更多详细信息,并在看到您的代码后:touchesMoved:withEvent
在平移已经开始之后调用,因此如果您更改那里的 MKMapView 中心,您将遇到您所描述的不稳定的渲染问题。你真正需要的是创建一个UIPanGestureRecognizer
带有目标动作,如下所示:
UIPanGestureRecognizer *pan = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizePan)] autorelease];
pan.delegate = self;
[self.mapView addGestureRecognizer:pan];
...然后添加一个didRecognizePan
方法到您的控制器,并在那里进行中心重置。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)