Search code examples
iphoneioscore-graphicscgaffinetransform

How can I prevent skew when scaling a UIView in one direction after rotation?


I'm rotating a UIView in the following manner:

CGAffineTransform currentTransform = [gestureRecognizer view].transform;    
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[[gestureRecognizer view] setTransform:newTransform]; 

and I would like to scale one dimension of this view (width or height) after rotation. However, if I do the following:

CGAffineTransformScale(self.parent.parentWidget.transform,sX, 1);

I end up with a skewed view. How do I prevent this and make it so that one dimension can be resized after rotation?

An example of this in action is in Keynote, where you can rotate an element, and then after rotation grab one selection handle and resize that element in just that direction.


Solution

  • Your problem is one of the order in which you are applying your transforms. If you attempt to scale your view in one dimension after it has been rotated, you won't produce the desired effect. Your scaling will occur in the X or Y dimension of the final, rotated object, not based on its original coordinate system.

    To achieve this effect, you'll need to create a transform where the scaling is applied before the rotation, then set that to your view. This may require you to track your UIView's rotation and scaling, so that you can regenerate a new transform every time one of these values changes.