CATransform3DRotate flip horizontal

I have a layer that I need to convert. I am currently using the following:

self.customLayer.transform = CATransform3DRotate(CATransform3DIdentity,M_PI / 2.0f, 0, 0, 1);

This correctly makes the layer right-side up, but it also needs to be flipped horizontally, as this is the wrong way. How to change CATransform3DRotatefor this?

+3
source share
3 answers

You will need:

self.customLayer.transform = CATransform3DScale(CATransform3DMakeRotation(M_PI / 2.0f, 0, 0, 1),
                                                -1, 1, 1);

A scale of -1 is a flip. Imagine you are twisting the image horizontally and you are walking past zero.

+9
source

I personally find it more readable to use KVC for these things. To achieve the same effect, you could use something like the following:

// Rotate the layer 90 degrees to the left
[self.customLayer setValue:@-1.5707 forKeyPath:@"transform.rotation"];
// Flip the layer horizontally
[self.customLayer setValue:@-1 forKeyPath:@"transform.scale.x"];
+4
source

CATransform3DScale (CATransform3D t, CGFloat sx, CGFloat sy, CGFloat sz)

If you want to flip horizontally, you should not specify a vector value in CATransform3DMakeRotation (). Instead, you just want to control the scale of the x axis.

By flipping it horizontally, you should:

self.transform = CATransform3DScale(CATransform3DMakeRotation(0, 0, 0, 0),
                                        -1, 1, 1);

If you want to flip it back to the beginning, follow these steps:

self.transform = CATransform3DScale(CATransform3DMakeRotation(0, 0, 0, 0),
                                        1, 1, 1);  

Addition
A shorter version will save you one operation. To flip:

self.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);

To return to normal:

self.transform = CATransform3DMakeRotation(0, 0, 1, 0);
+3
source

All Articles