2011-04-13 35 views
2

我有一个相当好奇的问题。UIView图层的animationForKey:始终为零

我认为我用CAKeyframeAnimation进行动画制作。然而,动画按预期工作,一旦代理的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块永远不会计算为真?为什么[someView.layer animationForKey:@"positionAnimation"]总是nil?此外,stringValue具有正确的数据,但数组也是nil

+0

我看到这个[说明](http://stackoverflow.com/questions/1255086/how-to-identify-caanimation-within-the-animationdidstop-delegate/5010291#5010291 );谁能证实这个理论 - 请吗? – haroldcampbell 2011-04-13 08:11:07

回答

2

我只能猜测,但也许当方法animationDidStop:finished:被称为你的动画已经从图层中删除。所以[someView.layer animationForKey:@"positionAnimation"]是零。

希望这会帮助

+1

谢谢 - 它的确如此。更具体地说,在将动画添加到图层之前,我必须调用方法'[positionAnimation setRemovedOnCompletion:NO];'。 所以看起来好像下面的情况一样:1:当动画被添加到图层时,它通过副本完成。 2:动画完成后,动画从图层中移除。 (恕我直言)最好直接在动画对象上设置KVO,然后在'animationDidStop:finished:'方法中检查该KV。 – haroldcampbell 2011-04-13 15:15:51