Home / iPad

The animation block

The UIView class provides common methods you use to create all types of views. In this particular code, it's used for a block of animation. The block-based animation method animateWithDuration:animations: (available only with iOS version 4.0 and newer) greatly simplifies creating an animation. With one method call, you can specify the animation and its options. (If your application runs on earlier versions of iOS, you must use the beginAnimations:context: and commitAnimations class methods to mark the beginning and ending of your animations.)

Inside the block, the code sets property values to make visual changes that comprise the animation. In this case, the code changes the rectangle's starting coordinates from startX to endX, and from -100 to self.view.

fallingImageView.frame = CGRectMake(endX, self.view.frame.
	    size.height, scaleW, scaleH);

The animateWithDuration:animations:completion method sets the animation duration and code to use upon completion. The completion code uses removeFromSuperview (an instance method of the UIView class) to remove fallingImageView from its superview, from its window, and from the responder chain; it then uses release (an instance method of the NSAutoreleasePool class) to release fallingImageView. Remember, you own any object you create with alloc, which means you're responsible for releasing it when you're done.

[Previous] [Contents] [Next]