Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 313
Mr Ordinary Avatar asked Jan 08 '13 13:01

Mr Ordinary


2 Answers

I did this and it worked perfect.

self.imageview.transform = CGAffineTransformIdentity
like image 170
nithinreddy Avatar answered Oct 22 '22 14:10

nithinreddy


(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)

like image 7
foundry Avatar answered Oct 22 '22 15:10

foundry