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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With