Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my CATransaction not honoring the duration I set?

I'm porting an iPhone app to Mac OS X. This code was being used successfully on the iPhone:

- (void) moveTiles:(NSArray*)tilesToMove {
    [UIView beginAnimations:@"tileMovement" context:nil];
    [UIView setAnimationDuration:0.1];  
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(tilesStoppedMoving:finished:context:)];

    for( NSNumber* aNumber in tilesToMove ) {
        int tileNumber = [aNumber intValue];
        UIView* aView = [self viewWithTag:tileNumber];
        aView.frame = [self makeRectForTile:tileNumber];
    }

    [UIView commitAnimations];
}

The Mac version uses CATransaction to group the animations, like so:

- (void) moveTiles:(NSArray*)tilesToMove {
    [CATransaction begin];
    [CATransaction setAnimationDuration:0.1];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [CATransaction setCompletionBlock:^{
        [gameDelegate tilesMoved];
    }];

    for( NSNumber* aNumber in tilesToMove ) {
        int tileNumber = [aNumber intValue];
        NSView* aView = [self viewWithTag:tileNumber];
        [[aView animator] setFrame:[self makeRectForTile:tileNumber]];
    }

    [CATransaction commit];
}

The animation is executing fine, except that the duration is 1.0 seconds. I can change the setAnimationDuration: call to anything, or omit it completely, and still the animation is 1.0 seconds in duration, every time. I also don't think the setAnimationTimingFunction: call is doing anything. However, setCompletionBlock: is working, because that block is executing when the animation completes.

What am I doing wrong here?

like image 583
zpasternack Avatar asked Nov 07 '10 19:11

zpasternack


1 Answers

If I am not mistaken you cannot use CoreAnimation to animate NSView's directly. For that you need NSAnimationContext and [NSView animator]. CATransaction will only work with CALayers.

like image 53
SteamTrout Avatar answered Oct 04 '22 21:10

SteamTrout