UIView图层的animationForKey:始终为nil

问题描述

|| 我有一个很奇怪的问题。 我认为我正在为ѭ0动画。动画按预期方式工作,但是,一旦激发了委托的
animationDidStop:finished
方法,则与该视图关联的图层在通过图层的
animationForKey:
方法进行检查时始终返回
nil
。 这是问题的说明:
// This is in the view controller
- (void) doAnimation:(id)sender {
    CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@\"position\"];

    [positionAnimation setValues:arrayOfPoint];
    [positionAnimation setDuration:0.5];
    // ViewController is the delegate and has the animationDidStop:finished: method
    positionAnimation.delegate = self;
    [positionAnimation setValue:@\"I am Text\" forKeyPath:@\"positionAnimation\"];

    [someView.layer addAnimation:positionAnimation forKey:@\"positionAnimation\"];
    someView.layer.position = newPosition;
}
在控制器的其他地方,我定义了
animationDidStop:finished:
方法,如下所示:
- (void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {

        NSArray *array = [someView.layer animationKeys];
        Nsstring *stringValue = [anim valueForKey:@\"positionAnimation\"];

        NSLog(@\"valueForKey: %@\",stringValue);
        // Prints \'I am Text\'

        if(anim == [someView.layer animationForKey:@\"positionAnimation\"]) {
            //THIS NEVER GET CALLED
        }
}
为什么if块永远不会评估为True?为什么
[someView.layer animationForKey:@\"positionAnimation\"]
总是
nil
?另外,
stringValue
具有正确的数据,但数组也是
nil
。     

解决方法

我只能猜测,但是也许调用方法
animationDidStop:finished:
时,您的动画已经从图层中删除了。所以
[someView.layer animationForKey:@\"positionAnimation\"]
为零。 希望这会有所帮助