Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? I've looked through the documentation and the Googles, but found nothing.

like image 639
thekevinscott Avatar asked Jun 15 '11 01:06

thekevinscott


2 Answers

And here's an easy to use Swift extension in case someone stumbles upon this in the future

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:

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)
    }
}
like image 101
kernelpanic Avatar answered Nov 02 '22 02:11

kernelpanic


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:

#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];
    }];
}
like image 38
Josh Gafni Avatar answered Nov 02 '22 04:11

Josh Gafni