UIView: how to "reset" transformations. CGAffineTransformScale not working as I would expect
Asked Answered
P

2

9

I think my question can be summed up as how to store and reset the transform of a view. But then perhaps explaining my situation might help.

If I apply the transforms below to a view, one after another (like if I add this code to a switch or a button). I get exactly the result I would expect: the scale switches between: a view that is .55 times the size of the original view, and the view at it's original scale. Works to scale sub-views of someView too, just as I want. Ad infinitum. Perfect.

//tranformScale 1
someView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.55, 0.55 );
//tranformScale 2
someView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0 );

The trouble is I want to use this code (or similar) to scale a sub-view of self.view when an iOS device goes into landscape, to fit the sub-view to the landscape screen (and back up when in portrait). It almost works, but for some reason instead of outputting round values values for the frame of the view being scaled (as happens with a test using a button to call the transforms), progressively strange values are produced. Eventually, after about 4 rotations the sub-view flies off screen. I suspect it has to do with self.view changing shape, but then again, when I log the frame of self.view it's shape is very predictable.

By the way I am centering the view using autoresizingMask flexible margins, not using auto-layout. perhaps I should be centering the view with another type of calculation?

Thanks for reading!

Pinniped answered 8/1, 2013 at 13:31 Comment(4)
You can just set someView.transform = CGAffineTransformIdentity; to reset it to no transform. It is easier to understand.Contrarious
Thanks, but even with that I get weird results.Pinniped
You know that the frame is bogus when a non-identity transform is applied right? From the documentation of frame on UIView: "Warning: If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored."Contrarious
David - can you clarify your statement? Does this mean that the transform has to be against the 'original' in order for the frame to be accurate (meaning that multiple/stacked transforms will yield bad results? I too am dealing with multiple transformations and getting unusual results too.Raffish
B
7

(1) be sure not to set or get any frame data after applying transforms, it is unsupported and will yield unpredictable results;

(2) turn off your autoresizing mask flex margins and center like this:

   view.center = CGPointMake(view.superview.bounds.size.width/2, 
                             view.superview.bounds.size.height/2);

(take care to apply centering while view is at full size. i.e. BEFORE the transform if it is the resize transform, AFTER the transform if it is the identity transform)

Bonzer answered 8/1, 2013 at 14:41 Comment(1)
Thanks that did it! Nice, miller time.Pinniped
T
34

I did this and it worked perfect.

self.imageview.transform = CGAffineTransformIdentity
Tachymetry answered 13/11, 2014 at 14:38 Comment(1)
if you dismissed that view by swiping your view does not re sacle to originalDeodorize
B
7

(1) be sure not to set or get any frame data after applying transforms, it is unsupported and will yield unpredictable results;

(2) turn off your autoresizing mask flex margins and center like this:

   view.center = CGPointMake(view.superview.bounds.size.width/2, 
                             view.superview.bounds.size.height/2);

(take care to apply centering while view is at full size. i.e. BEFORE the transform if it is the resize transform, AFTER the transform if it is the identity transform)

Bonzer answered 8/1, 2013 at 14:41 Comment(1)
Thanks that did it! Nice, miller time.Pinniped

© 2022 - 2024 — McMap. All rights reserved.