Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GMSMarker opacity animation not repeating

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?

like image 468
Theo Avatar asked Mar 27 '14 04:03

Theo


Video Answer


1 Answers

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.

like image 171
sanna Avatar answered Sep 28 '22 03:09

sanna