vikrant Rohilla
vikrant Rohilla

Reputation: 11

Flip a scaled and rotated uiview in objective c

I am trying to flip a scaled and rotated uiview on the horizontal axis. Here is the code being used -

CGFloat xScale = selectedFrame.transform.a;
CGFloat yScale = selectedFrame.transform.d;
selectedFrame.layer.transform = CATransform3DScale(CATransform3DMakeRotation(M_PI, 0, 1, 0),xScale, yScale,-1);

The output of this is that the view flips properly and the original scale factor is also manitained but the rotation isnt. Here are the images to explain the problem -

Original Image (The tiger view has to be flipped on the horizontal axis) - http://cloudart.com.au/projects/appscreenshots/Original_image.png

Flipped image after above code (see that the scale is maintained but the rotation angle isnt and the image s flipped properly) - http://cloudart.com.au/projects/appscreenshots/Flipped_image.png

Any help would be really appreciated!

Upvotes: 0

Views: 457

Answers (1)

FeichengMaike
FeichengMaike

Reputation: 448

To flip the image you are using a rotation of M_PI around the Y axis. To rotate the image, you need to apply another different rotation around the Z axis. These are two different transforms. You can combine them using CATransform3DConcat. Then you can scale the resulting transform.

CATransform3D transform = CATransform3DConcat(CATransform3DMakeRotation(zRotation, 0, 0, 1),CATransform3DMakeRotation(M_PI, 0, 1, 0));
[layer setTransform:CATransform3DScale(transform,xScale, yScale,1)];

The problem with your original code is that you are only applying the Y axis transform.

I'm sure there is a more elegant way to do this, but this works on my simulator.

Upvotes: 2

Related Questions