27

The documentation for Google Maps for iOS states that:

Call one of several methods that allow you to animate the camera moving to a new location. You can control the duration of the animation with CoreAnimation.

For the life of me, I can't figure out how to control the animation duration. I have tried using UIView animations, like:

    [UIView animateWithDuration: 5 animations:^{
         GMSCameraPosition *camera = [self newCamera];
        self.mapView.camera = camera;
    } completion:^(BOOL finished) {
    }];

And I have looked at CALayer animations in CoreAnimation. However, I don't know how you would apply a layer animation to the map view.

Can someone point me in the right direction please?

Colin Phillips
  • 971
  • 9
  • 11

4 Answers4

41

I found the answer ... you can control the animation duration by wrapping one of the animate* methods in a CATransaction, like this:

   [CATransaction begin];
   [CATransaction setValue:[NSNumber numberWithFloat: 1.0f] forKey:kCATransactionAnimationDuration];
   // change the camera, set the zoom, whatever.  Just make sure to call the animate* method.
   [self.mapView animateToCameraPosition: [self newCamera]];
   [CATransaction commit];
Colin Phillips
  • 971
  • 9
  • 11
14

for Swift 3.0:

CATransaction.begin()
CATransaction.setValue(1.5, forKey: kCATransactionAnimationDuration)
// your camera code goes here, example:
// mapView.animate(with: update)
CATransaction.commit()

The bigger the value (1.5 in this case), the slower the animation.

lenooh
  • 8,806
  • 5
  • 50
  • 44
6

Swift 2.0

CATransaction.begin()
CATransaction.setValue(NSNumber(float: 1.0), forKey: kCATransactionAnimationDuration)
// change the camera, set the zoom, whatever.  Just make sure to call the animate* method.
CATransaction.commit()
Shlomi Schwartz
  • 11,238
  • 25
  • 93
  • 155
2

what a pitty that using the same methods you provided there is no way to know if the animation has ended.

Yes I know, there is a CATransaction completion block using this method but it does simply not work! :(

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat: 1.0f] forKey:kCATransactionAnimationDuration];

[CATransaction setCompletionBlock:^{
// ... whatever you want to do when the animation is complete
}];

[self.googleMapsView animateToCameraPosition:[GMSCameraPosition 
                    cameraWithLatitude:LATITUDE
                             longitude:LONGITUDE
                                  zoom:ZOOM]];

[CATransaction commit];

And I can't use MapView:didIdle hack to know that the animation has ended because it will not be called if there is no camera position change.

Anyone knows how to detect animateon has ended event?

FOUND A THREAD ABOUT THIS (solved): CATransaction completion being called immediately

Community
  • 1
  • 1
Yaro
  • 1,202
  • 10
  • 15