GMSMarker opacity animation not repeating

回眸只為那壹抹淺笑 提交于 2019-12-09 12:06:43

问题


I'm trying to make a GMSMarker with a custom icon blink with a decaying animated opacity. The animation should repeat itself for a few times, but it does not, it just performs one transition and then it stops. This only happens when animating the opacity property, it works fine when animating other properties.

Here is the code:

GMSMarkerLayer* layer = marker.layer;
CABasicAnimation *blink = [CABasicAnimation animationWithKeyPath:@"opacity"];
blink.fromValue = [NSNumber numberWithFloat:0.0];
blink.toValue = [NSNumber numberWithFloat:1.0];
blink.duration = 1.0; 
blink.autoreverses = YES;
blink.repeatCount = 4;   
[layer addAnimation:blink forKey:@"blinkmarker"];

Documentation says I should be able to animate opacity since it is one of the properties GMSMarkerLayer allows us to.

Am I doing something wrong or is it a known bug I just ran into?


回答1:


My solution was to add a delegate to the animation:

CABasicAnimation *blink = [CABasicAnimation animationWithKeyPath:@"opacity"];
blink.fromValue = [NSNumber numberWithFloat:1.0];
blink.toValue = [NSNumber numberWithFloat:0.0];
blink.duration = 1.5;
[blink setDelegate:self];
[placeMarker.layer addAnimation:blink forKey:@"blinkmarker"];

And then when the animation has finish I get a callback and add it again:

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (flag) {
        CABasicAnimation *blink = [CABasicAnimation animationWithKeyPath:kGMSMarkerLayerOpacity];
        blink.fromValue = [NSNumber numberWithFloat:1.0];
        blink.toValue = [NSNumber numberWithFloat:0.0];
        blink.duration = 1.5;
        [blink setDelegate:self];
        [placeMarker.layer addAnimation:blink forKey:@"blinkmarker"];
    }
}

I had to do this since the GMSMarkerLayer does not care about repeating the animation. I tried reusing the animation in the callback and adding it again but that did not work.



来源:https://stackoverflow.com/questions/22678575/gmsmarker-opacity-animation-not-repeating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!