Controlling Animation Duration in Google Maps for iOS
Asked Answered
G

4

30

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?

Gosse answered 27/3, 2013 at 15:2 Comment(0)
G
44

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];
Gosse answered 27/3, 2013 at 15:43 Comment(0)
G
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.

Greed answered 6/12, 2016 at 18:2 Comment(0)
M
7

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()
Morna answered 27/1, 2016 at 21:49 Comment(0)
L
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

Labonte answered 31/10, 2015 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.