views:

56

answers:

1

I must be doing something wrong obviously. My delegate doesn't get called when I set the frame of a view.

Frame version NOT working

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

Weirdly enough; the same code using alphaValue does work:

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

Can you tell me what I am missing here? I just don't understand. Thank you for your time.

+1  A: 

You either use the animator or you use an explicit animation. In your case, since you want -animationDidStop to be called, don't use the animator. Also, you shouldn't be trying to animate the frame in an explicit animation. Either animate the bounds or animate the position or you can animate both by adding them both to your view's layer. Change your code to something like this:

-(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];
}

Since this is OSX, you will have to make sure your view's layer is layer backed. You do this by calling:

[[currentViewController view] setWantsLayer:YES];
Matt Long