2010-08-01 77 views
2

我一定是做错了显然。 当我设置视图的框架时,我的委托没有被调用。核心动画代表不叫

框架版本不工作

-(void)someMethod { 
    CAAnimation *anim = [CABasicAnimation animation]; 
    [anim setDelegate:self]; 
    [[currentViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"frame"]]; 
    [[newViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"frame"]]; 

    [[[currentViewController view] animator] setFrame:currentTarget]; 
    [[[newViewController view] animator] setFrame:newTarget]; 
} 

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag { 
    NSLog(@"Yep; works!"); 
} 

古怪不够;相同的代码使用alphaValue确实有效:

-(void)someMethod { 
    CAAnimation *anim = [CABasicAnimation animation]; 
    [anim setDelegate:self]; 
    [[currentViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"alphaValue"]]; 
    [[newViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"alphaValue"]]; 

    [[[currentViewController view] animator] setAlphaValue:0.5]; 
    [[[newViewController view] animator] setAlphaValue:0.5]; 
} 

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag { 
    NSLog(@"Yep; works!"); 
} 

你能告诉我我在这里失踪了吗?我只是不明白。 谢谢你的时间。

回答

1

您可以使用动画制作器使用显式动画。在你的情况下,因为你想-animationDidStop被调用,所以不要使用动画。另外,你shouldn't be trying to animate the frame in an explicit animation。要么为动画边界设置动画位置,要么通过将它们添加到视图的图层来设置动画。改变你的代码是这样的:

-(void)someMethod 
{ 
    CABasicAnimation *boundsAnim = 
         [CABasicAnimation animationWithKeyPath:@"bounds"]; 
    [boundsAnim setDelegate:self]; 
    [boundsAnim setFromValue:[NSValue valueWithRect:currentTarget]]; 
    [boundsAnim setToValue:[NSValue valueWithRect:newTarget]]; 

    CABasicAnimation *positionAnim = 
         [CABasicAnimation animationWithKeyPath:@"position"]; 
    [positionAnim setDelegate:self]; 
    [positionAnim setFromValue:[NSValue valueWithPoint:currentPosition]]; 
    [positionAnim setToValue:[NSValue valueWithRect:newPosition]]; 

    [[[currentViewController view] layer] addAnimation:boundsAnim forKey:nil]; 
    [[[currentViewController view] layer] addAnimation:positionAnim forKey:nil]; 
} 

因为这是OSX,你将不得不确保你的视图的图层是层支持。你可以通过拨打电话:

[[currentViewController view] setWantsLayer:YES]; 
2

可以使用动画设置器激活委托方法来设置帧。要做到这一点,你添加动画的单独frameSizeframeOrigin键:

-(void)someMethod { 
    CAAnimation *anim = [CABasicAnimation animation]; 
    [anim setDelegate:self]; 

    [[currentViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"frameOrigin"]]; 
    [[currentViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"frameSize"]]; 
    [[newViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"frameOrigin"]]; 
    [[newViewController view] setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"frameSize"]]; 

    [NSAnimationContext beginGrouping]; 
    [[[currentViewController view] animator] setFrame:currentTarget]; 
    [[[newViewController view] animator] setFrame:newTarget]; 
    [NSAnimationContext endGrouping]; 
} 

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag { 
    NSLog(@"Yep; works!"); 
} 

在这个例子中你的委托方法将火四个动画。如果您未重新调整大小而重新定位视图,则无需使用frameSize动画。