views:

32

answers:

1

Hi all,

I am trying to get the following code from a book to work. It animates the layer of a UIImageView called the Chicken :

[CATransaction setValue:kCAMediaTimingFunctionEaseInEaseOut forKey:kCATransactionAnimationTimingFunction];
[CATransaction setValue:[NSNumber numberWithFloat:1.0] forKey:kCATransactionAnimationDuration];
theChicken.position = CGPointMake(225, 144);

The problem is that this code does not animate. It just moves the chicken to 225,144 with no animation at all (it just teleports there like magic). Can anyone help me correct the code so it works ?

Thanks in advance,

Martin

A: 

What happens if you wrap it in:

[CATransaction begin];

[CATransaction setValue:kCAMediaTimingFunctionEaseInEaseOut forKey:kCATransactionAnimationTimingFunction];
[CATransaction setAnimationDuration:1.0];
theChicken.position = CGPointMake(225, 144);

[CATransaction commit];

If it still doesn't work it could be to do with the layer delegate. Is this layer a sub-layer? I think the delegate is set to nil on root layers which would have this affect.

You can use a UIView animation instead:

[UIView beginAnimations:@"anim1" context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

theChicken.position = CGPointMake(225, 144);

[UIView commitAnimations];
Ben
It still warps over there like magic :(
Ohnomycoco
I think this is a sub-layer ? It is an UIImage view I have created with the interface builder ?
Ohnomycoco
Then its just the views main layer. You should use UIView animations instead. I'll edit above.
Ben
I put this in and it crashes :[theChicken.layer setDelegate:self];
Ohnomycoco
Ah ok I see - that's the first way the book explained. Could I make the chicken a sub layer ?
Ohnomycoco
You could but it is probably simpler to animate the view with UIView
Ben