Thursday, May 5, 2011

Add CALayer as sublayer and then animate

Hi, here is what I want to do: Add a CALayer as a sublayer to antoher layer and then immediately animate it. The layer is added with it's position outside the current view and should "fly" into the view. This all takes place in a UIViewController.

Here's how my code basically looks like:

mysublayer.position = CGPointMake(-160.0, -250.0);
[self.view.layer addSublayer:mysublayer]; 

[CATransaction begin]; // outer transaction
[CATransaction setValue:[NSNumber numberWithFloat:2.0f]
     forKey:kCATransactionAnimationDuration];

mysublayer.position = CGPointMake(160.0, 250.0);
mysublayer.opacity = 1.0;

[CATransaction commit];

What happens is, that the sublayer immediately appears at the new position (160,250) with no animation. I think I'm doing something fundamental wrong and at the moment where I try to animate the layer it is not really added to the parent layer, right?

So, to wrap it up, how do I animate the new sublayer after adding it to a parent layer?

Thanks and kind regards, Hans

From stackoverflow
  • You should use [CABasicAnimation animationWithKeyPath:@"position"]

    Haentz : Thanks a lot. Got it basically working with a keyframe aniamtion.

0 comments:

Post a Comment