【问题标题】:Controlling Animation Duration in Google Maps for iOS在 iOS 版 Google 地图中控制动画持续时间
【发布时间】:2013-03-17 17:29:23
【问题描述】:
iOS 版 Google 地图的文档指出:
调用多种方法之一,使您可以将相机移动到新位置。您可以使用 CoreAnimation 控制动画的持续时间。
对于我的生活,我无法弄清楚如何控制动画持续时间。我尝试过使用 UIView 动画,例如:
[UIView animateWithDuration: 5 animations:^{
GMSCameraPosition *camera = [self newCamera];
self.mapView.camera = camera;
} completion:^(BOOL finished) {
}];
我在 CoreAnimation 中查看了 CALayer 动画。但是,我不知道如何将图层动画应用到地图视图。
有人能指点我正确的方向吗?
【问题讨论】:
标签:
ios
animation
google-maps-sdk-ios
【解决方案1】:
我找到了答案...您可以通过将其中一个 animate* 方法包装在 CATransaction 中来控制动画持续时间,如下所示:
[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];
【解决方案2】:
对于 Swift 3.0:
CATransaction.begin()
CATransaction.setValue(1.5, forKey: kCATransactionAnimationDuration)
// your camera code goes here, example:
// mapView.animate(with: update)
CATransaction.commit()
值越大(本例中为 1.5),动画越慢。
【解决方案3】:
斯威夫特 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()
【解决方案4】:
很遗憾,使用您提供的相同方法无法知道动画是否结束。
是的,我知道,有一个使用此方法的 CATransaction 完成块,但它根本不起作用! :(
[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];
而且我不能使用 MapView:didIdle hack 来知道动画已经结束因为如果没有相机位置变化,它就不会被调用。
有人知道如何检测动画已结束事件吗?
找到了一个关于这个的线索(已解决):
CATransaction completion being called immediately