简体   繁体   English

MKMapView 的 setRegion 速度

[英]speed of setRegion for MKMapView

If I change the region in setRegion for an MKMapView , is there a way to set the speed, or duration, of that animation change?如果我在setRegion中更改MKMapView的区域,有没有办法设置 animation 更改的速度或持续时间? I've looked through the documentation and the Googles, but found nothing.我浏览了文档和谷歌,但一无所获。

And here's an easy to use Swift extension in case someone stumbles upon this in the future这是一个易于使用的 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)
    }
}

Update to Swift 5:更新到 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)
    }
}

I was able to set the duration of the setRegion animation by editing the response to the question - Setting the zoom level for a MKMapView - as follows:我可以通过编辑对问题的响应来设置setRegion animation 的持续时间 - 设置 MKMapView 的缩放级别- 如下:

#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];
    }];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM