views:

85

answers:

1

Hi guys,

This one has been driving me nuts...

Considering "plane" is a CALayer, I rotate it in the X axis:

plane.transform = CATransform3DMakeRotation(180 * M_PI / 180.0f, 1.0f, 0.0f, 0.0f);

And this makes it rotate clockwise, which is perfect.

Now, at some point in time, I want it to return to 0 degrees, so I use:

plane.transform = CATransform3DMakeRotation(0 * M_PI / 180.0f, 1.0f, 0.0f, 0.0f);

The thing is... it does it anti-clockwise, which is not what I want :(

I'm guessing it uses the shortest rotation path, but even if I tell it to go to 360 degrees instead, when it's done and I tell it to restart the animation, it starts from 360 to go to 180, and it goes backwards instead of the right direction.

Is there a way to workaround that?

What I'd do in Actionscript would be:

if (plane.rotationX == 360) plane.rotationX = 0;

And it'd resume nicely, but if I do that using CATransform3DMakeRotation both transformations collide, because of the animation (I think) and it gets messed up.

Any help would be much appreciated!

Thanks

+1  A: 

I think your problem is down to expecting a little too much of the implicit animation system, which is basically just going to interpolate your layer property values from A to B. This is great for a wide variety of cases, but it doesn't allow you to do things like "chain" changes together in sequence. There are ways to do the latter, though they're definitely less convenient than what you're doing at the moment. Still, you might want to investigate those to do things "properly". (Start with another look at the Core Animation Guide.)

However, if you're willing to put up with a cheesy workaround, you might be able to get this to look how you want just by continuing to change the rotation angle in the same direction each time rather than setting it to a constant 0 or 180:

// you need to keep track of this somewhere in your code
// (I've made it static for simplicity, you should do something more sensible)
static long currentAngle = 0;

// rotate (another) 180 degrees
currentAngle += 180;
plane.transform = CATransform3DMakeRotation(currentAngle * M_PI / 180.0f, 1.0f, 0.0f, 0.0f);

I say "might", because something in your description doesn't really chime with how I would expect the transform to work, and there are clearly some implementation subtleties I don't properly understand. This approach may well fail. But it's trivially simple, so probably worth a try.

walkytalky
Thank you for your help. Unfortunately I'd tried that already, only it didn't work because my planes can go both ways and the cumulative rotation gets ruined. As a reference, this is what im trying to replicate: www.assalino.com (the 3D stack after the intro). I did it in AS3 and it's relatively simple. The key is being able to change the rotation from -180 to 180 (for example) without animation. The only way I've found to do it in Core Animation is by changing the duration property to 0 for that change and then setting it back for the final animation, but they overlap and it doesn't work.
Andre
@Andre Your Flash didn't work for me (and really, life's too short), so perhaps I'm missing something, but I'm not sure how the planes going both ways should affect the cumulative rotation - if it works one way, it should work the other. In any case, if this hack won't do, I think you'll need to forego implicit animation and use explicit instead -- you can stop and start those at will, specify sequences of values and timing, get notification when they complete, etc.
walkytalky