在iOS6中控制MKMapView的动画速度

时间:2012-10-08 16:18:58

标签: iphone ios animation mkmapview ios6

我正试图在地图视图上关注汽车。

此代码应以相同的速度为汽车和地图设置动画,以便注释视图始终显示在中心:

[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中,地图不再是动画,但汽车确实有动画效果。

我试过[mapView setCenterCoordinate:co animated:YES],但后来无法控制动画速度。它将始终使用默认持续时间(0.2秒)进行动画处理。

3 个答案:

答案 0 :(得分:11)

我今天遇到了同样的问题。我认为这个问题并不依赖于MKMapView,而是(新)方式ios6管理动画。

似乎在ios6中,如果动画在前一个动画完成之前发生(取决于运行循环),旧动画会被新动画中断。我认为这只会在使用“beginFromCurrentState”选项或属性(取决于你是否使用基于块的动画)时使用(由新的)。

可以肯定的是,我认为你应该尝试使用基于块的动画来查看你的动画是否真的被另一个动画打断了。

此代码必须与您的代码等效,并允许您查看动画是否已被中断或取消(如果“已完成”为假):

[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中,“完成”应该是真的......)

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

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

答案 1 :(得分:7)

我发现标准的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)
    })

答案 2 :(得分:4)

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

- (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;

但是我找到了一个具有持续时间参数的私有方法。所以我通过继承MKMapView并重写私有方法(仅存在于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个允许您覆盖默认动画时间的属性。 要使用它,请在更改地区之前将overridesAnimationDuration设置为YES,将mapAnimationDuration设置为所需的持续时间,并在委托致电overridesAnimationDuration中将mapView:regionWillChangeAnimated:切换为否。

请注意,这可能不适用于未来的iOS版本,因为它是私有API。 Apple可以删除或更改此方法。