如何在委托和释放控制器中识别CAAnimation?

时间:2010-12-15 16:33:08

标签: iphone objective-c core-animation core-graphics

看过How to identify CAAnimation within the animationDidStop delegate?,这是对它的补充。

我无法正常工作。我有一个动画,我想释放它在动画结束后运行的控制器。 示例:控制器从右侧转换 - >然后离开然后发布。

定义动画:

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];

委托方法内部:

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

    if (theAnimation == check)
    {
        //[check release];
        [self release];

    }
}

如果我按原样保留此代码,则我的控制器不会被释放(由于动画保留调用)。 如果我运行[check release],我会获得message sent to deallocated instance

有谁知道什么是错的? 是否有另一种方法可以在animationDidStop委托中识别CAAnimation而不指定removedOnCompletion = NO

编辑: 忘了提。通过不指定removedOnCompletion = NOanimationForKey:将返回NULL。因此我无法识别动画。

谢谢!

2 个答案:

答案 0 :(得分:4)

我认为最终的原因是CAAnimation.delegate是一个保留属性(非常奇怪的oops!)。

头文件定义是:

/* 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;

要让self自我释放,必须从图层中删除动画,例如:

[self.view.layer removeAnimationForKey:@THE_ANIMATION_KEY];

答案 1 :(得分:3)

目前还不清楚您的问题是什么,但它可能会帮助您了解CAAnimation个实例是通用的KVO容器,因此您可以向其添加自定义信息:

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

然后您可以检查:

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

这有帮助吗?