views:

56

answers:

2

Hi,

I have a problem very similar to this one :

http://stackoverflow.com/questions/805502/catransaction-not-animating

I'm just trying to animate a view layer using CATransaction. My problem is that the transform is applied to the view immediatly.

I tried to perform the animation using performSelector:withObject:afterDelay: without success.

Here is my code :

- (void)viewDidLoad {
    [super viewDidLoad];

 view = [[[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)] autorelease];
 view.backgroundColor = [UIColor blackColor];
 [self.view addSubview:view];

 [self performSelector:@selector(animateView) withObject:nil afterDelay:0.1];
}

-(void) animateView {
 [CATransaction begin];
 [CATransaction setValue:[NSNumber numberWithFloat:3.0f] forKey:kCATransactionAnimationDuration];
    CALayer *layer = view.layer;
    layer.position = CGPointMake(20, 
         300);
    CATransform3D transform = CATransform3DMakeScale(2.0f, 2.0f, 1.0f);
    transform = CATransform3DRotate(transform, acos(-1.0f)*1.5f, 1.5f, 1.5f, 1.5f);
    layer.transform = transform;
    [CATransaction commit];
}

Does anybody knows what is going wrong ?

Thanks, Vincent.

+1  A: 

when animating a view's backing layer, you need to be inside a UIView animation block, not just a CATransaction:

[UIView beginAnimations:nil context:NULL];
// ... your animation code
[UIView commitAnimations];
Jason Foreman
A: 

or you could use CAAnimationGroup and group the scale and rotation animations like this..

CAAnimationGroup *aniGroup = [CAAnimationGroup animation]; // setup aniGroup properties

CABasicAnimation *scaleAnimation = [CABasicAnimation defaultValueForKey:@"transform.scale"]; scaleAnimation.fromValue = [NSNumber numberWithFloat:0.0]; scaleAnimation.toValue = [NSNumber numberWithFloat:1.0]; // setup scaleAnimation properties

CABasicAnimation *rotateAnimation = [CABasicAnimation defaultValueForKey:@"transform.rotate"]; // setup rotateAnimation properties // ....

aniGroup.animations = [NSArray arrayWithObjects:rotateAnimation, scaleAnimation, nil];

[self.layer layoutIfNeeded]; [self.layer addAnimation:aniGroup forKey:@"myAnimation"];

Something like that.... Might not be exactly copy, paste, and build-able. ;)

Hope this helps!

Brandon Emrich