Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS, setTransform overridden by setFrame? Transform not re-drawing after setFrame is re-run

My transform does not draw after the frame is redrawn with setFrame.

I'm scaling a view when the orientation changes using setFrame. But this view also needs to change position depending on a BOOL argument: On = up in view, off = down off screen. I use setTransform to adjust the position of the view.

First I draw the view by doing a setFrame. This draws the view 100 points just off screen at the bottom. Then I set a transform (-100 on the ty) to bring it up into the view points (as long as the BOOL is set to TRUE, else the transform is 0 and the view is off screen).

[view setFrame:CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, 100)];
[view setTransform:CGAffineTransformMake(1, 0, 0, 1, 0, -100)]

This works fine, as expected, but when change orientation, and this code is re-run (to re-size the view) the transform does not draw, even though it is Logged as being the transform of the view. In other words the view is just off screen, as if the transform.ty was 0.

Log message before re-draw: view.transform.ty -10.000000
Log message after re-draw: view.transform.ty -10.000000

Any help?

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

Mr Ordinary


1 Answers

A view's frame is a value that is derived from three fundamental values: bounds, center, and transform. The setter for frame tries to do the right thing by reversing the process, but it can't always work correctly when a non-identity transform is set.

The documentation is pretty clear on this point:

If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.

...

If the transform property contains a non-identity transform, the value of the frame property is undefined and should not be modified. In that case, you can reposition the view using the center property and adjust the size using the bounds property instead.

Since your transform only translates the view, I don't see any reason to use it at all. Just change the origin of the view's frame:

[view setFrame:CGRectMake(0, self.view.bounds.size.height - 100, self.view.bounds.size.width, 100)];
like image 88
Kurt Revis Avatar answered Oct 05 '22 20:10

Kurt Revis