Search code examples
iphoneobjective-ccocoa-touchuiviewcgaffinetransform

setTransform:CGAffineTransformMakeScale - Alternatives when trying to scale an UIView


I use the following to scale a view to 3x its original size:

[pageShadowView setTransform:CGAffineTransformMakeScale (3, 1)];

I would like to have a method which tests if my UIView (pageShadowView) is already scaled - and in that case I'd like to divide it by 3, so that it is back to its normal size.

I can't think of any method which would do that. So I thought it may be perhaps best to test if my UIPageView has a certain dimension, and if it doesn't to rescale it to its original size.

So my question is if there is a method to scale an UIView to a certain width and height which is not relative (i.e. 3x its size), but expressed in pixel (e.g. 200 pixel x 300 pixel). I couldn't find any thing in the documentation which is, to be honest, a bit over my head when it comes to the section on CGAffineTransform.

Any suggestions would be very much appreciated!


Solution

  • To check if your view is scaled we just need to check if its transform is not an identity transform.

    BOOL isScaled = ! CGAffineTransformIsIdentity(pageShadowView.transform);
    

    One should note, that this check will only be valid if you do not use any other kinds of transform i.e. rotation or translation.

    Scaling UIView to certain width and height is easy as well:

    CGFloat yourDesiredWidth = 200.0;
    CGFloat yourDesiredHeight = 300.0;
    
    CGAffineTransform scalingTransform;
    scalingTransform = CGAffineTransformMakeScale(yourDesiredWidth/pageShadowView.bounds.size.width, 
                                                  yourDesiredHeight/pageShadowView.bounds.size.height);
    pageShadowView.transform = scalingTransform;
    

    It is important to check UIView's bounds instead of frame, because frame is not valid when UIView is transformed.