17.15. Animating and Scaling Views

Problem

You want to be able to animate the scaling up or down of your views.

Solution

Create a scale affine transformation for your view and use the UIView animation methods to animate the scale transformation.

Discussion

Note

I highly recommend that you read Recipe 17.14 before proceeding with this section of the book.

In order to scale a view while animating it, you can either apply a scale transformation to it within an animation block (see Recipe 17.12), or just increase the view’s width and/or height.

Let’s have a look at scaling an image view by applying a scale transformation to it:

- (void) viewDidAppear:(BOOL)paramAnimated{
  [super viewDidAppear:paramAnimated];

  /* Place the image view at the center of the view of this view controller */
  self.xcodeImageView.center = self.view.center;

  /* Make sure no translation is applied to this image view */
  self.xcodeImageView.transform = CGAffineTransformIdentity;

  /* Begin the animation */
  [UIView beginAnimations:nil
                  context:NULL];

  /* Make the animation five seconds long */
  [UIView setAnimationDuration:5.0f];

  /* Make the image view twice as large in
   width and height */
  self.xcodeImageView.transform = CGAffineTransformMakeScale(2.0f,
                                                             2.0f);

  /* Commit the animation */
  [UIView commitAnimations];

}

This code uses an affine scale transformation to scale the image view to become twice as big as it originally was. The best thing about applying scale transformations to a view is that the width and height are scaled using ...

Get iOS 6 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.