Drawing

Drawing into a UIView allows you to create an infinite variety of lines, shapes, images, text, colors and patterns and even pdf content.

Apple's chief references are the Drawing and Printing Guide for iOS and the Quartz 2D Programming Guide.

All the drawing is done by the drawRect routine in UIView. For things like UIControl and UIImage the drawing is done automatically by the system, and you normally don't do anything more. For custom subclasses of UIView you will implement drawRect, but you do not call it. Rather, the system tracks the state of all views. When one first appears or is covered and re-exposed, it will call drawRect to update the view. If you change the view and want it redrawn, you call setNeedsUpdate on the view and the system will cause drawRect to be called.

Drawing is done into a "context" which is associated with the UIView. The context holds things like the current color, the pen size, the transform matrix and a number of other things. The UIView has the physical bounds and a transform matrix of its own. The context is used as a parameter in most of the drawing calls.

Each step of drawing can be transformed in several ways. There is a Current Transform Matrix (CTM) which produces an "affine transformation" on the drawing. The math is not that bad, but you can look it up on Wikipedia. Briefly, an affine transformation can scale, translate and rotate the drawing. You apply the transformation before, not after, calling the draw routine.

In addition there are ways to blend one drawing over another. You can change the alpha (transparency) of the drawing and make it blend with the existing content in many ways. Transparency and blending apply to the thing being drawn against whatever is already in the view. In other words, you should draw opaque things first, then anything to be blended on top of them.