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!

OWolf
  • 5,012
  • 15
  • 58
  • 93
  • 1
    You can just set `someView.transform = CGAffineTransformIdentity;` to reset it to no transform. It is easier to understand. – David Rönnqvist Jan 08 '13 at 14:16
  • Thanks, but even with that I get weird results. – OWolf Jan 08 '13 at 14:23
  • 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." – David Rönnqvist Jan 08 '13 at 14:37
  • 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. – wayneh Jun 01 '14 at 14:47

2 Answers2

34

I did this and it worked perfect.

self.imageview.transform = CGAffineTransformIdentity
nithinreddy
  • 6,167
  • 4
  • 38
  • 44
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)

foundry
  • 31,615
  • 9
  • 90
  • 125