Search code examples
objective-cioscore-animationcalayer

Core Animation implicit animation doesn't fire for newly added sublayer


Don't know why this doesn't work. I can't do implicit animation for a newly added sublayer until some even triggers it like a button push or being called from another method. This here below doesn't animate even though I think i should. It just shows the 'after' state of the layer.

    CAGradientLayer *newBar = [CAGradientLayer layer];

    CGFloat barHeight = ((pair.amount.floatValue/self.model.maxModelValue.floatValue)*maxBarHeight);
    newBar.bounds=R(0,0,self.barWidth,0); 
    newBar.anchorPoint=P(0, 1);
    newBar.position=P((i*barSpacing), self.insideLayer.bounds.size.height);
    newBar.backgroundColor=self.lowerColor.CGColor;
    newBar.colors=[NSArray arrayWithObjects:(id)self.upperColor.CGColor, self.lowerColor.CGColor, nil];
    newBar.cornerRadius=self.cornerRadius;
    newBar.opacity=1.0;
    [self.insideLayer addSublayer:newBar];

    //animation line:
    newBar.opacity=0.5;

Solution

  • Core Animation won't create an implicit animation if the layer isn't part of the presentation layer hierarchy.

    Try this:

    ...
    [self.insideLayer addSublayer:newBar];
    
    // Tell Core Animation to update the presentation layer hierarchy:
    [CATransaction flush];
    
    // animation line:
    newBar.opacity = 0.5;