简体   繁体   中英

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;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM