views:

58

answers:

1

I wish to remove a CALayer from its superlayer without animating. What happens here is the layer animates to a position, works great, when however the animation stopped, this code is executed, which returns the layer to its start position, and fades out; presumably then gets removed from the superlayer. How may it be stopped from animating -removeFromSuperlayer ? The code listed here has the same behavior for all variations of the included comments being uncommented and not uncommented, Transaction or no transaction. What am I missing?

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    //[self setHidden: YES];

    //[CATransaction flush];
    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue
                     forKey:kCATransactionDisableActions];
    //[CATransaction setDisableActions: YES];
    //[CATransaction setAnimationDuration: 0];
    [self removeFromSuperlayer];
    [CATransaction commit];
}

I have been searching around, and this code is not any different from what I have found.

+1  A: 

You can disable the implicit animation by setting the actions dictionary on the superlayer to return null for animations involving sublayers (similar to my answer here):

NSMutableDictionary *newActions = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSNull null], @"sublayers", nil];
superlayer.actions = newActions;
[newActions release];

You may also need to override the layer's (not the superlayer's) onOrderOut action to prevent this. I show how to do that in the linked answer.

Brad Larson
Excellent! I had no idea about Actions until now, thank you! Now the layers are not returning to initial position, and fading - only now returning to initial position briefly (like flicker, added both 'sublayers' and 'onOrderOut'). May I add another question here, how to leave the layers where they ended up after they were animated? Thanks again!~ (vote up if i could)
humasect
@humasect - Try setting the animation's fillMode to kCAFillModeForwards and removedOnCompletion to NO.
Brad Larson
Wow, that is some work to remove some layers after animating! But I will assume the defaults are better for more common tasks, and this works perfectly -- thank you !! +1
humasect