iosobjective-cmkmapviewcatransaction

How to slow MKMapCamera movement?


I am coding in iOS.

I have an NSArray, which contains a few MKMapCameras. I want to display MKMapCameras from the array one after another.

I put a while loop and used [self.mapView setCamera:nextCamera animated:YES];

However, this is only showing the first and the last views. Everything in between is going too fast.

I want to slow down the movement of each camera. Is there a way to achieve it using CATransaction or using any other animation tricks. If so, could you please show me an example code?

Want to give an update... I tried below code. But it isn't working... Camera movements are fast as I mentioned earlier.

[CATransaction begin];
[CATransaction setAnimationDuration:5.5];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[CATransaction setCompletionBlock:^{
    [self.mapView setCamera:nextCamera animated:YES];
}];
[CATransaction commit];

Solution

  • After fiddling with it a few hours, I figured out a way to make it work. Thought of sharing the same with everyone...

    I made two changes. I replaced CATransaction with UIView's animation. I also removed Camera's default animation settings, which was conflicting with UIView's animation.

    Below is the code.

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:2.5];
    self.mapView.camera = nextCamera;
    [UIView commitAnimations];