David
David

Reputation: 1318

Moving Animation of a CALayer?

I have a array of CALayers that i'm looping and trying to move.

Tile is a CALayer subclass and has a CGRect property named originalFrame where i store the frame i want to animate to.

When i'm useing the code below everything is instant moved to the correct possition and there is no 4 sec animation. How can i animate these CALayer?

       for (int i = 0; i < [tileArray count]; i++) {
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationDelay:i];
            [UIView setAnimationDuration:4];
            Tile *currentCard = (Tile*)[tileArray objectAtIndex:i];
            currentCard.frame = currentCard.originalFrame;
            [UIView commitAnimations];
        }

Upvotes: 1

Views: 401

Answers (1)

erikprice
erikprice

Reputation: 6308

You have two problems: the first is that you're trying to animate the layer's frame directly. Since this is a derived property, you can't do that. Instead, you have to animate the position property. http://developer.apple.com/library/mac/#qa/qa1620/_index.html

Second, you're using UIView's +beginAnimations API, but you say your Tile objects are CALayers, not UIViews. So you don't need to use +beginAnimations. Instead you need to use a CAAnimation object, like CABasicAnimation (untested):

for (Tile *tile in tileArray)
{
    static NSString * const kProperty = @"position";

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:kProperty];
    animation.duration = 4.0f;
    animation.fromValue = [tile valueForKey:kProperty];
    animation.toValue = [NSValue valueWithCGRect:tile.originalFrame];
    [tile addAnimation:animation forKey:kProperty];
}

Upvotes: 3

Related Questions