我从您提到的链接下载了 XCode 项目,并且能够重现该错误。以下答案有一个对我有用的解决方法
MKMapView 没有在 Pan 上调用 regionDidChangeAnimated
为方便起见,我想重复解决方案以及我如何在上述项目中应用它
在CustomCalloutViewController.h
添加UIGestureRecognizerDelegate
@interface CustomCalloutViewController : UIViewController
<MKMapViewDelegate, UIGestureRecognizerDelegate>
在CustomCalloutViewController.m
方法中viewDidLoad
添加之前[super viewDidLoad];
if (NSFoundationVersionNumber >= 678.58){
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureCaptured:)];
pinch.delegate = self;
[mapView addGestureRecognizer:pinch];
[pinch release];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureCaptured:)];
pan.delegate = self;
[mapView addGestureRecognizer:pan];
[pan release];
}
然后仍然在CustomCalloutViewController.m
添加以下
#pragma mark -
#pragma mark Gesture Recognizers
- (void)pinchGestureCaptured:(UIPinchGestureRecognizer*)gesture{
if(UIGestureRecognizerStateEnded == gesture.state){
///////////////////[self doWhatYouWouldDoInRegionDidChangeAnimated];
}
}
- (void)panGestureCaptured:(UIPanGestureRecognizer*)gesture{
if(UIGestureRecognizerStateEnded == gesture.state){
NSLog(@"panGestureCaptured ended");
// *************** Here it is *********************
///////////////////[self doWhatYouWouldDoInRegionDidChangeAnimated];
// *************** Here it is *********************
}
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch: (UITouch *)touch{
return YES;
}
编辑:我在这里找到了另一种解决方法(在底部也提到了上述解决方法)。我没有尝试,但听起来很有希望。我在这里重复一遍:
我的解决方法很简单:在您的视图控制器中,在 viewDidAppear: 中创建 MKMapView,并在 viewDidDisappear: 中销毁它。我意识到这对于那些使用 Interface Builder 的人来说不是一个友好的解决方法,但是,在我看来,它是最干净的,并且可能是在你的应用程序中节省内存的最好方法。