1

I have a UIView to which various scale and rotation CGAffineTransforms have been applied, in response to touch screen gestures.

When a scale operation completes, I want to adjust the bounds of the view to the new size, then have view to re-draw itself at a scale of 1.0, while keeping all other components of the transform the same.

To adjust the bounds of the view, I'm using:

self.myView.bounds = CGRectApplyAffineTransform(self.myView.bounds, self.myView.transform);

To "undo" the scale transform, I'm trying this:

self.myView.transform = CGAffineTransformConcat(self.myView.transform, CGAffineTransformMakeScale(1, 1));

then calling [myView setNeedsDisplay] to get the view to redraw itself.

However this does not produce the desired results and when a rotate transform is applied, the above code seems to cause what looks like a sideways translation transform to be applied too.

What's the cleanest way to "undo" just a scale transform and have the view redraw at 1:1 with all other transforms remaining intact?

Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75

2 Answers2

0

I am handling something similar, and what I do is store the separate components in another struct (scale, rotation, translation). That way you can easily recreate the transform again (be careful about the order of your operations though).

One thing though: I suggest that you don't change the bounds and instead just apple a scale operation to the transform. This will avoid some potential unneeded layouts. All of this stuff can be handled purely with the transform property. The way you are doing it now is not going to change anything since applying a scale of 1 is a no-op. If you scaled up the view by 2, you would need to scale by 0.5 to get back to the original. Or, as I said above, store all the components and recreate it from the identity matrix (matrix math is fast, don't worry about that).

borrrden
  • 33,256
  • 8
  • 74
  • 109
  • 1
    Thanks borrrden. The reason I'm changing the bounds is I want the view to redraw itself without any scale transform so that contents are sharp, rather than blurred, as when a scale transform is applied. – Andrew Ebling Jul 17 '13 at 14:26
  • If you are using CoreGraphics to draw the view, you could try setting the contentScale on the layer to match the scale. In my experience that results in sharp redrawing. If you insist on using the bounds method, then you must store the scale elsewhere to you can undo it (divide the bounds width and height by the scale). You will also probably have to redo all the operations since matrix multiplication is *not* communicative. – borrrden Jul 17 '13 at 14:31
0

Save your scale values in a variable and then, try changing CGAffineTransformMakeScale(1, 1) into

CGAffineTransformMakeScale(1/totalScaleWidth, 1/totalScaleHeight)
Efesus
  • 197
  • 2
  • 17