简体   繁体   English

iOS GoogleMaps SDK-animateToCameraPosition动画完成处理程序?

[英]iOS GoogleMaps SDK - animateToCameraPosition animation finished handler?

Currently I am using the GoogleMaps SDK for iOS for various operations. 目前,我正在使用iOS版GoogleMaps SDK进行各种操作。 When calling 打电话时

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

is there a completion handler to determine wether the animation finished or not? 是否有完成处理程序来确定动画是否完成?

Of course I get with the GMSMapViewDelegate updates about the cameraPosition but how should I check if the animation finished? 当然,我可以通过GMSMapViewDelegate获得有关cameraPosition的更新,但是如何检查动画是否完成?

- (void)mapView:(GMSMapView *)mapView 
didChangeCameraPosition:(GMSCameraPosition *)position;

为便于以后的读者参考,2013年7月发布的iOS版Google Maps SDK 1.4.0已添加了一个新的委托方法mapView:idleAtCameraPosition:它将在任何照相机运动结束时触发-无论是像程序动画一样在这个问题或用户触发的动作。

This might work (I haven't tried it): 这可能有效(我没有尝试过):

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat: 1.0f] forKey:kCATransactionAnimationDuration];
[self.googleMapsView animateToCameraPosition:[GMSCameraPosition 
                        cameraWithLatitude:LATITUDE
                                 longitude:LONGITUDE
                                      zoom:ZOOM]];
[CATransaction setCompletionBlock:^{
    // ... whatever you want to do when the animation is complete
}];
[CATransaction commit];

Basically, this creates an animation transaction that animates your camera movement (change the value for numberWithFloat: to change the speed) and you set your own completion block stating what you want to do when the animation is over. 基本上,这将创建一个动画事务,该动画事务使摄像机的运动动画化(更改numberWithFloat:的值以更改速度),然后设置自己的完成块,说明动画结束后要执行的操作。 [CATransaction commit] is what fires the animation off. [CATransaction commit]会触发动画。

Note: this answer partially based off this answer . 注意:此答案部分基于此答案

I don't believe there is, however... 我不相信有...

A method that has worked well for me so far is to set a timer to fire (very) shortly after the location stops updating: 到目前为止,对我来说效果很好的一种方法是在位置停止更新后不久设置一个计时器(非常)触发:

- (void)mapView:(GMSMapView*)mapView didChangeCameraPosition:(GMSCameraPosition*)position {
  // _panTimer is an instance variable of the delegate.
  [_panTimer invalidate];
  _panTimer = [NSTimer timerWithTimeInterval:0.2
                                      target:self
                                    selector:@selector(_mapHasStoppedMoving)
                                    userInfo:nil
                                     repeats:NO];
  [[NSRunLoop currentRunLoop] addTimer:_panTimer forMode:NSDefaultRunLoopMode];
}

I came across the issue of google's animation methods lacking completion handlers recently. 我最近遇到了缺少完成处理程序的Google动画方法的问题。
The best solution I've found so far is to attach my own completion handler via CATransation API. 到目前为止,我找到的最好的解决方案是通过CATransation API附加我自己的完成处理程序。

private func attachCompletionHandlerToGoogleAnimations(@noescape animations: () -> Void, #completion: (() -> Void)!) {
    CATransaction.begin()
    CATransaction.setCompletionBlock(completion)
    animations()
    CATransaction.commit()
}

Example usage: 用法示例:

attachCompletionHandlerToGoogleAnimations({
    googleMapView.animateToLocation(coordinate)
}) {
    println("camera moved to location \(coordinate)")
}

SWIFT version example: SWIFT版本示例:

let vancouver = CLLocationCoordinate2D(latitude: 49.26, longitude: -123.11)
let calgary = CLLocationCoordinate2D(latitude: 51.05,longitude: -114.05)
let bounds = GMSCoordinateBounds(coordinate: vancouver, coordinate: calgary)
let cameraPosition = GMSCameraUpdate.fit(bounds)

CATransaction.begin()
CATransaction.setValue(1.0/*duration in seconds*/, forKey: kCATransactionAnimationDuration)
CATransaction.setCompletionBlock({
    print("animation complete, do whatever you want here")
})
mMapView.animate(with: cameraPosition)
CATransaction.commit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM