views:

998

answers:

2

I am using CGAffineTransformMake to flip an UIImageView vertically. It works fine but it does not seem to save the new flipped position of UIImageview, because when I try to flip it 2nd time (execute the line code below) it just does not work.

shape.transform = CGAffineTransformMake(1, 0, 0, 1, 0, 0);

help please.

Thanks in advance.

Kedar

A: 

Hi Kedar,

If you just want to reverse the effects of a previous transformation, you may like to look into setting the shape.transform property to the value CGAffineTransformIdentity.

When you set a view's transform property you are replacing any existing transform it has, not adding to it. So if you assign a transform which causes a rotation, it will forget about any flip you had previously configured.

If you want to add an additional rotation or scaling operation to a view which you have previously transformed you should investigate the functions which allow you to specify an existing transform.

I.e. instead of using

shape.transform = CGAffineTransformMakeRotation(M_PI);

which replaces the existing transform with the specified rotation, you could use

shape.transform = CGAffineTransformRotate(shape.transform, M_PI);

this applies the rotation to the existing transform (what ever that may be) and then assigns it to the view. Take a look at Apple's documentation for CGAffineTransformRotate, it may clarify things a little.

Christopher Fairbairn
You may like to also look into functions such as CGAffineTransformScale (which allow you to create a transform by using an existing transform as a starting point).
Christopher Fairbairn
Hi Christopher, Thanks for the response. My problem, as I mentioned in the original post is, I am trying to flip the image vertically (mirror image of the image inside UIImageview). The code snippet I have posted in the original post works fine but it does not save the new (flipped) state or transform of the UIImageview. Because once flipped I want to do further rotations on that flipped image. Say I flip and then rotate, it flips the UIImageview with the code above and when I click rotate it rotates the original UIImageview not the flipped one. Hope i did not confuse you.
Kedar
+1  A: 

Transforms are not automatically additive/accumulative as you would expect. Assigning a transform just transforms the target once.

Each transform is highly specific. If apply a rotation transform that rotates a view +45 degrees, you will see it rotate only once. Applying the same transform again does not rotate the view an additional +45 degrees. All subsequent applications of the same transforms produce no visible effect because the view is already rotated +45 degrees and that is all that transform will ever do.

To make transforms accumulative you have apply the new transform to the existing transform instead of just replacing it. So as mentioned previously for each subsequent rotation you use:

shape.transform = CGAffineTransformRotate(shape.transform, M_PI);

Which adds the new transform to the existing transform. If you add a +45 degree transform in this manner the view will rotate an additional +45 each time it is applied.

TechZen
Hi TechZen, yes it works that way for rotation. But my problem here is not with rotation. I am trying to flip (mirror image) an UIImageview (please see the code i am using for this in my original post) is there any way I can make that transform permanent? Thanks for your help. I appreciate it.
Kedar
When you say, "because when I try to flip it 2nd time" do you mean apply the exact same transform a second time? If so, my original advice still stands. If not, then I've misunderstood what you're trying to do,
TechZen