Drawing model

Drawing in iOS and Mac OS X follows one of two technology paths and is based on a model in which views update their appearance on demand. To draw your application’s content you can use either OpenGL or the native rendering facilities of the platform.

OpenGL rendering often makes use of the view classes defined by the application frameworks UIKit and AppKit; when it does, some aspects of the drawing model are similar to that for native rendering. However, OpenGL applications do not have to be based on views.

Note: The information that follows describes aspects of the drawing model for native rendering. For information about OpenGL and OpenGL ES, consult the OpenGL website at http://www.opengl.org/.

A View Draws Itself When Necessary in Its drawRect: Method

A major element of the drawing model is content updating on demand. A view draws its content in the drawRect: method, which the view base class (UIView and NSView) declares and a view subclass overrides. The rectangle passed into the method identifies the area of the view that needs to be redrawn. The first time a view appears in a window, the rectangle matches the bounds of the view and the view draws all of its content. Subsequently, the view draws itself only when necessary and draws only that part of itself that has changed.

image: Art/drawing_model.jpg

Several actions can trigger a view update:

At the end of a cycle of the main event loop, a window proceeds down its view hierarchy and requests these views to draw themselves by calling their drawRect: methods.

A Graphics Context Sets the Drawing Environment

UIView and NSView automatically configure the drawing environment of a view before its drawRect: method is invoked. (In the AppKit framework, configuring the drawing environment is called locking focus.) As part of this configuration, the view class creates a graphics context for the current drawing environment.

This graphics context is a Quartz object (CGContext) that contains information the drawing system requires, such as the colors to apply, the drawing mode (stroke or fill), line width and style information, font information, and compositing options. (In the AppKit, an object of the NSGraphicsContext class wraps a CGContext object.) A graphics context object is associated with a window, bitmap, PDF file, or other output device and maintains information about the current state of the drawing environment for that entity. A view draws using a graphics context associated with the view’s window. For a view, the graphics context sets the default clipping region to coincide with the view’s bounds and puts the default drawing origin at the origin of a view’s boundaries.

Drawing Takes Place in a View’s Local Coordinate System

A view draws itself in its local coordinate system. The view’s bounds property serves to define the location of points in this coordinate system.

You can change a view’s default coordinate system by modifying the current transformation matrix (CTM). The CTM maps points in a view’s coordinate system to points on the device’s screen. By changing the CTM, you can modify the size, orientation, and position of a view’s coordinate system relative to its superview or window, thereby scaling, rotating, or moving the view.

Drawing commands make reference to a fixed-scale drawing space, known as the user coordinate space. The operating system maps coordinate units in this drawing space onto the actual pixels of the corresponding target device. Thus vector commands issued by a view scale to any resolution supported by the device. These commands specify drawing coordinates using floating-point values to acquire the precision required for vector-based drawing.

Prerequisite Articles

Related Articles

Sample Code Projects

Did this document help you? Yes It's good, but... Not helpful...


Last updated: 2010-07-07