MKMapView的setRegion速度

时间:2011-06-15 01:37:24

标签: iphone ios ipad mkmapview region

如果我更改setRegionMKMapView的区域,是否有办法设置该动画更改的速度或持续时间?我查看了文档和谷歌,但一无所获。

2 个答案:

答案 0 :(得分:18)

这是一个易于使用的Swift扩展程序,以防将来有人偶然发现

import MapKit

extension MKMapView {
    func animatedZoom(zoomRegion zoomRegion:MKCoordinateRegion, duration:NSTimeInterval) {
        MKMapView.animateWithDuration(duration, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseIn, animations: {
        self.setRegion(zoomRegion, animated: true)
        }, completion: nil)
    }
}

更新到Swift 5:

extension MKMapView {
    func animatedZoom(zoomRegion:MKCoordinateRegion, duration:TimeInterval) {
        MKMapView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: UIView.AnimationOptions.curveEaseIn, animations: {
            self.setRegion(zoomRegion, animated: true)
            }, completion: nil)
    }
}

答案 1 :(得分:6)

我可以通过编辑对问题的回复 - Setting the zoom level for a MKMapView来设置setRegion动画的持续时间 - 如下所示:

#import <MapKit/MapKit.h>

@interface MKMapView (ZoomLevel)

- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
                   zoomLevel:(NSUInteger)zoomLevel
                   animated:(BOOL)animated;
@end



#import "MKMapView+ZoomLevel.h"

@implementation MKMapView (ZoomLevel)

#define ANIMATION_DURATION 0.5
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
                      zoomLevel:(NSUInteger)zoomLevel animated:(BOOL)animated {
    MKCoordinateSpan span = MKCoordinateSpanMake(0, 360/pow(2,zoomLevel)*self.frame.size.width/256);
    [MKMapView animateWithDuration:ANIMATION_DURATION animations:^{
        [self setRegion:MKCoordinateRegionMake(centerCoordinate, span) animated:YES];
    }];
}