How do I set CATransformLayer animation center of rotation

553 views Asked by At

I have a view with it's underlying layer, and I add a CATransformLayer to the view's layer:

[view.layer addSublayer:transformLayer];

My added layer contains all sorts of sublayers representing individual sides of several 3 dimensional blocks. I would like to apply an animation to a layer to rotate the blocks.

CATransform3D rotation = CATransform3DMakeRotation(deg * M_PI / 180, x, y, z);
[CATransaction begin];

    [(CATransformLayer *)[pView.layer.sublayers objectAtIndex:0] setSublayerTransform:rotation];
        or (both seem to produce the same result)
    [(CATransformLayer *)[pView.layer.sublayers objectAtIndex:0] setTransform:rotation];

[CATransaction commit];

The problem is the layer is rotating roughly around the front corner instead of the center of the cubes (see below for example).

                    / \                  / \        
   / \            / \ / \              / \ / \      
 / \ / \         |\ / \ /|            |\ / \ /|     
|\ / \ /|        | |\ /| |            | |\ /| |     
| |\ /| |         \| | |/ \    OR    / \| | |/      
|\| | |/|  ==>      \|/ \ / \      / \ / \|/        
|1|\|/|B|            |\ / \ /|    |\ /   /|         
 \|2|A|/             |A|\ /| |    | |\ /|2|         
   \|/                \|B| |/      \| |1|/          
                        \|/          \|/            

The A/B labels should move to the 1/2 positions, or 1/2 should move to A/B, depending on the direction of rotation. Instead it changes from the first figure, rotates roughly around the front corner, resulting in something close to the other figures. The upper unlabeled blocks are in a different view and static so not part of the rotation. The lower labeled blocks are in the view/layer I am trying to animate.

How do I change the center of rotation to achieve my desired result?

I have messed with anchorPoint, zPosition, anchorPointZ for various layers with no success. Have tried setSublayerTransform, setTransform as well.

Should I be messing with settings for the block side sublayers, the transform layer, the view layer, or the view itself to achieve what I am after?

Any guidance or direction would be appreciated.

1

There are 1 answers

1
GRW On BEST ANSWER

UPDATE:

I have finally come to the realization and accepted the fact that I can not do this using Core Animation.

I have gone down the road of using OpenGL to solve this problem.


This problem has slowly evolved as I make improvements bit by bit, but the end result involves making the parent layer a CATransformLayer, which retains the 3D perspective, and rotating that.

As far as the center of rotation, I use CATransform3DMakeTranslation to change the position of the layer.