Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling the animation speed of MKMapView in iOS6

I'm trying to follow a car on a map view.

This code should animate the car and the map with the same speed, so that the annotation view always appears in the center:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration: 1.0];
[UIView setAnimationBeginsFromCurrentState:YES];

[car setCoordinate:coord];
[mapView setCenterCoordinate:coord];

[UIView commitAnimations];

It worked fine in iOS 5. In iOS 6 the map is not animating anymore but the car does animate.

I tried [mapView setCenterCoordinate:co animated:YES], but then I cannot control the animation speed. It will always animate with the default duration (0.2s).

like image 415
Felix Avatar asked Oct 08 '12 16:10

Felix


1 Answers

I ran into the same kind of issue today. I think the problem does not rely on MKMapView, but the (new) way ios6 manages animations.

It seems that in ios6, if an animation occurs before a previous one has finished (depending on the run loop), the older gets interrupted by the new one. I think this only occurs if the "beginsFromCurrentState" option or property (depending if you are using block based animation or not) is used (by the new one).

To be sure, I think you should try to use block-based animation to see if your animation is really interrupted by another one.

This code must be equivalent to yours, and allows you to see if your animation has been interrupted or cancelled (if "finished" is 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);
}];

(in iOS < 6,"finished" should be true...)

In my case, it appeared that my animation was interrupted by the following UIViewController's methods, which was performed by the system in an animation block, interrupting my animation chain :

- (void)viewWillAppear:(BOOL)animated;
- (void)viewDidAppear:(BOOL)animated;
like image 158
polo987 Avatar answered Sep 24 '22 14:09

polo987