Graphics Programs Reference
In-Depth Information
This is your first time programmatically creating an instance of a view and adding it as a
subview of another view, but you have been doing the same thing all along in XIB files. In
your XIB files, when you dragged a view from the library onto the canvas, you created the
view instance. When you dragged that view on top of another view, you established a sub-
view/superview relationship between those two views. A view created programmatically
and a view created by loading a XIB file are no different once the application is executing.
When creating a view programmatically, you use
alloc
and an initializer message like
you would for any other object. The designated initializer of
UIView
, and thus
Hyp-
nosisView
, is
initWithFrame:
. This method takes a
CGRect
structure as an argu-
ment. This
CGRect
is the view's
frame
.
Every view instance has a
frame
rectangle. A view's frame specifies the view's size and
position relative to its superview. A frame is represented by a
CGRect
structure and con-
The
origin
is of type
CGPoint
and contains two
float
members:
x
and
y
. The
size
is of type
CGSize
and has two
float
members:
width
and
height
. (A struc-
ture is not an Objective-C object, so you can't send it messages, and you don't declare it
as a pointer.)
Figure 6.6 CGRect
Thus, a view is always a rectangle. Because the
HypnosisView
's
origin
is
(160,
240)
, its top left corner is 160 points to the right and 240 points down from the top-left
corner of the window (its superview). This places the top-left corner of the
Hypnos-