How to define CAAnimation in delegate and release controller?

I saw How to define CAAnimation in the animationDidStop delegate? is an addition to it.

I cannot get this to work properly. I have an animation, and I would like to release a controller that was launched after the animation ended. Example: the controller moves from right → left, then frees itself.

Animation Definition:

NSValue *end = [NSValue valueWithCGPoint:CGPointMake(800, self.view.center.y)]; NSValue *start = [NSValue valueWithCGPoint:self.view.center]; CABasicAnimation *moveAnimation; moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; moveAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; moveAnimation.duration = 0.45f; moveAnimation.fromValue = start; moveAnimation.toValue = end; // actually set the position [self.view.layer setPosition:[end CGPointValue]]; moveAnimation.delegate = self; moveAnimation.removedOnCompletion = NO; [self.view.layer addAnimation:moveAnimation forKey:MOVING_OUT]; 

Inside the delegate method:

 - (void) animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag { CAAnimation *check = [self.view.layer animationForKey:MOVING_OUT]; if (theAnimation == check) { //[check release]; [self release]; } } 

If you leave this code as it is, my controller will not receive dealloc'd (due to holding the call by animation). If I run [check release] , I get a message sent to deallocated instance .

Does anyone know what happened? Is there any other way to identify CAAnimation in the animationDidStop delegate WITHOUT specifying removedOnCompletion = NO ?

EDIT: Forgot to mention. Without indicating that removedOnCompletion = NO , animationForKey: will return NULL. Therefore, I cannot identify the animation.

Thanks!

+4
source share
2 answers

It's not clear that your problem is here, but it can help you find out that CAAnimation instances are universal KVO containers, so you can add custom information to them:

 [myAnimation setValue: @"check" forKey: @"name"]; 

Then you can check this:

 if ([[theAnimation valueForKey: @"name"] isEqual: @"check"]) // ... 

Does it help?

+3
source

I think a possible reason is CAAnimation.delegate - a persistence property (very strange!).

Header file definition:

 /* The delegate of the animation. This object is retained for the * lifetime of the animation object. Defaults to nil. See below for the * supported delegate methods. */ @property(retain) id delegate; 

To enable self get release, the animation must be removed from the layer, for example:

 [self.view.layer removeAnimationForKey:@THE_ANIMATION_KEY]; 
+4
source

Source: https://habr.com/ru/post/1332239/


All Articles