Proper transform for scaling a UIView from a rect? Proper transform for scaling a UIView from a rect? objective-c objective-c

Proper transform for scaling a UIView from a rect?


So actual implementation is very simple:

- (CGAffineTransform)translatedAndScaledTransformUsingViewRect:(CGRect)viewRect fromRect:(CGRect)fromRect {    CGSize scales = CGSizeMake(viewRect.size.width/fromRect.size.width, viewRect.size.height/fromRect.size.height);    CGPoint offset = CGPointMake(CGRectGetMidX(viewRect) - CGRectGetMidX(fromRect), CGRectGetMidY(viewRect) - CGRectGetMidY(fromRect));    return CGAffineTransformMake(scales.width, 0, 0, scales.height, offset.x, offset.y);}

You can see full example here: https://github.com/vGubriienko/TransformTest

(I still need update it to use identity as full image)


If you're only dealing with images, you can take advantage of the UIImageView's contentMode property and set it to UIViewContentModeScaleAspectFill. Then you can just do a [UIView transitionWithView:] and set the frame of your UIImageView to your final CGRect (or your whole screen because it will fill correctly). It will do the zooming-in effect you're looking for.

The best way to do this is to create a copy of the UIImageView as a subview of your superview and place it right on top of the original UIImageView and perform the transformations from there.

I wrote some functions to extend it to a UITableViewController a while back which lets you zoom-in on any UIImageView with a fade to black and zoom transition and the final image is zoomable and can be dismissed.

https://github.com/PeterCen/iOS-Image-Presenter/


Turns out the best solution was to create a transform manually using a matrix.