O'Reilly logo

iOS 7 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

20.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 20.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 20.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 5 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required