Take the 2-minute tour ×

I've got a problem which I can't seem to solve.

I'd like to scale my application's view so that it appears that a view has 'zoomed in' so that it maintains its aspect ratio and fills the entire width of the screen.

I (think) I have some math to make this work, but I'm not sure how to apply this in a CGAffineTransform statement, or how to center the view.

Step 1:
Scale the view:

float scaleFactor = (320 / boxWidth);

self.view.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);

Step 2:
Position the view so it appears it's centered:

CGRect newFrame = self.view.frame;

newFrame.x = 20;
newFrame.y = mainBox.frame.origin.y;    

self.view.frame = newFrame;

And this is where I'm stuck. I'm not sure how to position the view so that it appears it's centered.

Here's an image to demonstrate what I'd like to achieve:


Bascially, I'd like the view to scale so that the black box animates into the position and size of the red box.

I'm a bit stuck on this, so any help is appreciated.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

In such cases it's easier to just calculate the final frame of the view and set it inside an animation block:

CGRect newFrame = self.view.frame;
newFrame.size.height *= 320.0f / newFrame.size.width;
newFrame.size.width = 320.0f;
newFrame.origin.x = 0.0f;
// Set newFrame.origin.y as desired

[UIView animateWithDuration: 0.5 animations: ^{
    self.view.frame = newFrame;
share|improve this answer
add comment

Your Answer


