Search code examples
iphoneobjective-ccocoa-touchcore-animationcore-graphics

Moving/Rotating an object using it's updated position and tilt?


This is basically a simple issue, which I can't get around ...

  1. So, I have an object of UIImageView of a certain frame over which, I implement CAAnimation with rotation and translation, and it is at new coordinates (x,y), and has been rotated by some degrees.

  2. This animation works beautifully. But if I again do a rotation and movement from THAT state, I want the object to use the new frame and new properties from STEP 1 after its rotation and again rotate by the new angle.

As of now, when I try rotation again, it uses its standard state & frame size(during initialization) and performs rotation on it...

And by this I mean... If I have a square of frame (100, 200, 10, 10), and I rotate it by 45 degrees, the shape is now a different square, with different frame size and end points compared to the original square, and I implement a new rotation by (say) 152 degrees on it and it needs to perform a rotation on the newer square... But it turns out that it uses the same frame size as the previous one (x,y, 10, 10).

How can I continue rotating / moving the object with its updated position and state ??


Note: (if you need to see the code for animation)

This is the code for my animation, which involves simple rotation and movement ! http://pastebin.com/cR8zrKRq


Solution

  • You need to save the rotation step and update object rotation in animationDidStop: method. So, in your cas, you should apply:

    -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
    { 
       //angle is global
       CATransform3D rotationTransform = CATransform3DMakeRotation((angle%4)*M_PI_4, 0, 0, 1);
       [object.layer setTransform:rotationTransform];
       object.center = tempFrame; //already there
    }
    

    where angle is an integer counter of animations(steps) with values 0,1,2,3. My step is M_PI_4. There is probably a better solution to the problem, but this should do the trick