在iOS6中控制MKMapView的animation速度

我试图在地图视图上跟踪一辆车。

该代码应该以相同的速度animation汽车和地图,以便注释视图总是出现在中心:

[UIView beginAnimations:nil context:NULL]; [UIView setAnimationCurve:UIViewAnimationCurveLinear]; [UIView setAnimationDuration: 1.0]; [UIView setAnimationBeginsFromCurrentState:YES]; [car setCoordinate:coord]; [mapView setCenterCoordinate:coord]; [UIView commitAnimations]; 

它在iOS 5中运行良好。在iOS 6中,地图不再是animation,但汽车具有animation效果。

我试过[mapView setCenterCoordinate:co animated:YES] ,但是我无法控制animation速度。 它将始终使用默认持续时间(0.2s)进行animation制作。

今天我遇到了同样的问题。 我认为这个问题不依赖MKMapView,但(新)的方式ios6pipe理animation。

看起来在ios6中,如果animation在前一个animation完成之前(取决于运行循环)发生,则较旧的animation会被新animation中断。 我认为这只会发生,如果“beginFromCurrentState”选项或属性(取决于如果您使用基于块的animation或不),使用(新的一个)。

可以肯定的是,我认为你应该尝试使用基于块的animation,看看你的animation是否真的被另一个中断。

此代码必须与您的代码相同,并允许您查看您的animation是否已被中断或取消(如果“已完成”为false):

 [UIView animateWithDuration:1.0 delay:0.0f options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionBeginFromCurrentState) animations:^{ [car setCoordinate:coord]; [mapView setCenterCoordinate:coord]; } completion:^(BOOL finished){ NSLog(@"has not been interrupted : %d", finished); }]; 

(在iOS中<6,“完成”应该是真的…)

在我的情况下,似乎我的animation被下面的UIViewController的方法中断,这是由系统在animation块中执行的,中断了我的animation链:

 - (void)viewWillAppear:(BOOL)animated; - (void)viewDidAppear:(BOOL)animated; 

我发现标准的UIView AnimationWithDuration将应用如果格式化如此:

 // create a region with a center coordinate and a degree span let center = CLLocationCoordinate2D(latitude: 42.3601, longitude: -71.0689) let span = MKCoordinateSpanMake(1.0, 1.0) let region = MKCoordinateRegion(center: center, span: span) UIView.animateWithDuration(3.0, delay: 0.0, options: .CurveEaseInOut | .AllowUserInteraction, animations: { self.mapView.setRegion(region, animated: true); }, completion: { finished in println("completed 3 second animation to new region") }) 

希望也适合你! 🙂

(注:有人指出,这个回应是针对iOS7 / 8的,而问题是针对iOS6的。)


而且,这个简单的实际上可以正常工作…

  let c = mkMap.userLocation.coordinate let r = CLLocationDistance( 1500 ) let cr = MKCoordinateRegionMakeWithDistance( c, r, r ) UIView.animate(withDuration: 1.4, animations: { [weak self] in self?.mkMap.setRegion( cr, animated: true) }) 

MKMapView使用这些方法时,似乎无法控制iOS 6中的animation速度:

 - (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated; - (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated; - (void)setVisibleMapRect:(MKMapRect)mapRect animated:(BOOL)animate; - (void)setVisibleMapRect:(MKMapRect)mapRect edgePadding:(UIEdgeInsets)insets animated:(BOOL)animate; 

不过,我发现一个具有持续时间参数的私有方法。 所以我解决了我的问题,通过inheritanceMKMapView并重写私有方法(只存在于iOS6中):

MyMapView.h

 #import <MapKit/MapKit.h> @interface MyMapView : MKMapView @property(nonatomic) BOOL overridesAnimationDuration; @property(nonatomic) NSTimeInterval mapAnimationDuration; @end 

MyMapView.m

 @interface MKMapView (Private) - (void)_setZoomScale:(float)scale centerMapPoint:(CLLocationCoordinate2D)center duration:(double)d animationType:(int)animType; @end @implementation MyMapView - (void)_setZoomScale:(float)scale centerMapPoint:(CLLocationCoordinate2D)center duration:(double)d animationType:(int)animType { if (_overridesAnimationDuration) { d = _mapAnimationDuration; } [super _setZoomScale:scale centerMapPoint:center duration:d animationType:animType]; } @end 

我已经添加了2个属性,允许您覆盖默认的animation时间。 要在区域更改之前将overridesAnimationDuration设置为YES,请将mapAnimationDuration设置为所需的持续时间,并在委托调用mapView:regionWillChangeAnimated: overridesAnimationDuration切换为NO。

请注意,这可能无法在未来的iOS版本,因为它是私人API。 苹果可以删除或更改此方法。