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{[superviewDidAppear: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 */[UIViewbeginAnimations:nilcontext:NULL];/* Make the animation 5 seconds long */[UIViewsetAnimationDuration:5.0f];/* Make the image view twice as large inwidth and height */self.xcodeImageView.transform=CGAffineTransformMakeScale(2.0f,2.0f);/* Commit the animation */[UIViewcommitAnimations];}
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access