未调用核心动画委托

时间:2010-08-01 13:07:26

标签: cocoa macos delegates core-animation

我显然必须做错事。 当我设置视图的框架时,我的委托不会被调用。

帧版本无法正常工作

-(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!");
}

你能告诉我这里缺少什么吗?我只是不明白。 谢谢你的时间。

2 个答案:

答案 0 :(得分: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动画。

答案 1 :(得分:1)

您可以使用animator 使用显式动画。在您的情况下,因为您想要调用 -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];