Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CAAnimation delegate methods not called

I am having an issue with iOS CABasicAnimation. No matter what I do, I cannot get the methods animationDidStart: and animationDidStop:finished: to fire. My class is subclassing CAShapeLayer and is performing the animations inside of it:

- (void)start{
    [self removeAllAnimations];

    CABasicAnimation *pathAnimation = [self makeAnimationForKey:@"strokeEnd"];
    [self addAnimation:pathAnimation forKey:@"strokeEnd"];
}

- (CABasicAnimation *)makeAnimationForKey:(NSString *)key {
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:key];
    anim.fromValue = [NSNumber numberWithFloat:0.f];
    anim.toValue = [NSNumber numberWithFloat:1.f];

    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    anim.duration = self.duration;
    anim.delegate = self;
    return anim;
}

- (void)animationDidStart:(CAAnimation *)anim{
    NSLog(@"HERE START");
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    NSLog(@"HERE STOP");
}

Any tips or help would be appreciated, thanks in advance!

like image 723
Austin Avatar asked May 11 '26 09:05

Austin


2 Answers

you must set delegate before assigning animation to layer:

popIn.delegate = self;
[annotation.layer addAnimation:popIn forKey:@"popIn"];

Swift 5.4.2

popIn.delegate = self
annotation.layer.add(transition, forKey:"popIn")
like image 99
Hashem Aboonajmi Avatar answered May 12 '26 22:05

Hashem Aboonajmi


Ok so it turns in my subclass I had a property called duration. Even though it is not documented as being apart of CALayer, duration is a part of one of it's protocols called CAMediaTiming. The methods were never fired because the property was being overwritten via my subclass.

like image 38
Austin Avatar answered May 12 '26 22:05

Austin