Graphics Programs Reference
In-Depth Information
Core Graphics
The functions and types that begin with
CG
come from the Core Graphics framework, a C
language API for 2D drawing. The hub of the Core Graphics framework is
CGContex-
tRef
: all other Core Graphics functions and types interact with a drawing context in some
way, and then the context creates an image.
Let's look at the Core Graphics functions you used in the implementation of
Hypnos-
isView
's
drawRect:
method. You set up the drawing state with
CGContex-
tSetLineWidth
and then set its color using
CGContextSetRGBStrokeColor
.
Next, you added a
path
to the context using
CGContextAddArc
. A path is a collection
of points that forms a shape and can form anything from a square to a circle to a human
outline. There are a number of Core Graphics functions like
CGContextAddArc
that you
can use to add a path to a context. (Search the documentation for
CGContextRef
to find
them.)
After a path has been added to a context, you can perform a drawing operation. There are
three drawing operations:
•
CGContextStrokePath
will draw a line along the path.
•
CGContextFillPath
will fill the shape made by the path.
•
CGContextClip
will limit future drawing operations to the area defined by the
path. For example, drawing a square to a context that has been clipped to a circle
of the same size will result in drawing a circle.
After a drawing operation, the current path is removed from the context. Thus, to draw
more than one circle, you have to add a path to the context for each circle. In
Hypnos-
isView.m
, modify
drawRect:
to draw a number of concentric circles.
- (void)drawRect:(CGRect)dirtyRect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect bounds = [self bounds];