How to remove a layer when its animation completes?

后端 未结 4 561
陌清茗
陌清茗 2020-12-19 13:28

I am making an iOS App. I have several CALayer objects that eventually will be deleted by a (shrinking) animation. When the animatio

4条回答
  •  青春惊慌失措
    2020-12-19 14:06

    One alternativ solution is to add a layer pointer to the animation object's dictionary, as follows

    // in some define section
    #define kAnimationRemoveLayer @"animationRemoveLayer"
    

    then, in the animationDidStop,

    - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{
        CALayer *lay = [theAnimation valueForKey:kAnimationRemoveLayer];
        if(lay){
            [lay removeAllAnimations];
            [lay removeFromSuperlayer];
        }
    }
    

    and, finally, in the animation setup,

    CALAyer * lay = ... ;
    BOOL    shouldRemove = .... ; 
    CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"position"];
    anim.delegate = self;
    if (shouldRemove)
        [anim setValue:lay forKey:kAnimationRemoveLayer];
    

提交回复
热议问题