UIView层的animationForKey:总是为零

时间:2011-04-13 07:55:35

标签: iphone objective-c uiview uiviewcontroller core-animation

我有一个相当奇怪的问题。

我认为我使用CAKeyframeAnimation制作动画。动画按预期工作,但是一旦触发了委托的animationDidStop:finished方法,当使用图层的nil方法检查时,与视图关联的图层始终返回animationForKey:

以下是问题的说明:

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

1 个答案:

答案 0 :(得分:2)

我可以猜测,但是当调用方法animationDidStop:finished:时,您的动画已经从图层中删除了。所以[someView.layer animationForKey:@"positionAnimation"]是零。

希望这会有所帮助