As you’d expect, geometry plays an
important role when working with views. Geometry defines where each
view appears onscreen, what its size is, and how it is oriented. The UIView class provides two built-in properties that define these aspects.
Every view uses a frame
to define its boundaries. The frame specifies the outline of the view:
its location, width, and height. If you change a view’s frame, the view
updates to match the new frame. Use a bigger width and the view
stretches. Use a new location and the view moves. The view’s frame
delineates each view’s onscreen outline. View sizes are not limited to
the screen size. A view can be smaller than the screen or larger. It
can also be smaller or larger than its parent.
Views also use a transform
property that sets the view’s orientation and any geometric
transformations that have been applied to it. For example, a view might
be stretched or squashed by applying a transform, or it might be
rotated away from vertical. Together the frame and transform fully
define a view’s geometry.
Frames
Frame rectangles use a CGRect structure, which is defined as part of the Core Graphics framework as its CG prefix suggests. A CGRect is made up of an origin (a CGPoint, x and y) and a size (a CGSize, width and height). When you create views, you normally allocate them and initialize them with a frame, for example:
CGRect rect = CGRectMake(0.0f, 0.0f, 320.0f, 416.0f);
myView = [[UIView alloc] initWithFrame: rect];
The CGRectMake
function creates a new rectangle using four parameters, the origin’s x
and y locations, the width of the rectangle, and its height. In
addition to CGRectMake, there are several other convenience functions you may want to be aware of that help you work with rectangles and frames.
NSStringFromCGRect(aCGRect) converts a CGRect structure to a formatted string. This function makes it easy to log a view’s frame when you’re debugging.
CGRectFromString(aString)
recovers a rectangle from its string representation. It proves useful
when you’ve stored a view’s frame as a string in user defaults and want
to convert that string back to a CGRect.
CGRectInset(aRect, xinset, yinset)
enables you to create a smaller or larger rectangle that’s centered on
the same point as the source rectangle. Use a positive inset for
smaller rectangles, negative for larger ones.
CGRectIntersectsRect(rect1, rect2) lets you know whether rectangle structures intersect. Use this function to know when two rectangular onscreen objects overlap.
CGRectCreateDictionaryRepresentation(aRect) transforms a rectangle structure into a standard CFDictionaryRef, also known (via the magic of toll-free bridging) as (NSDictionary *) instances. Transform the dictionary back to a rectangle by using CGRectMakeWithDictionaryRepresentation(aDict, aRect).
CGRectZero is a rectangle constant located at (0,0)
whose width and height are zero. You can use this constant when you’re
required to create a frame but are still unsure what that frame size or
location will be at the time of creation.
The CGRect structure is made up of two substructures: CGPoint, which defines the rectangle’s origin, and CGSize, which defines its bounds. Points refer to locations defined with x and y coordinates; sizes have width and height. Use CGPointMake(x, y) to create points. CGSizeMake(width, height)
creates sizes. Although these two structures appear to be the same (two
floating-point values), the iPhone SDK differentiates between them.
Points refer to locations. Sizes refer to extents. You cannot set myFrame.origin to a size.
As with rectangles, you can convert them to and from strings: NSStringFromCGPoint(), NSStringFromCGSize(), CGSizeFromString(), and CGPointFromString() perform these functions. You can also transform points and sizes to and from dictionaries.
Transforms
The
iPhone supports standard affine transformations as part of its Core
Graphics implementation. Affine transforms allow points in one
coordinate system to transform into another coordinate system. These
functions are widely used in both 2D and 3D animations. The version
used in the iPhone SDK uses a 3-by-3 matrix to define UIView
transforms, making it a 2D-only solution. With affine transforms, you
can scale, translate, and rotate your views in real time. You do so by
setting the view’s transform property, for example:
float angle = theta * (PI / 100);
CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
myView.transform = transform;
The
transform is always applied with respect to the view’s center. So when
you apply a rotation like this, the view rotates around its center. If
you need to rotate around another point, you must first translate the
view, then rotate, and then return from that translation.
To revert any changes, set the transform property to the identity transform. This restores the view back to the last settings for its frame.
myView.transform = CGAffineTransformIdentity;
Coordinate Systems
Views
live in two worlds. Their frames are defined in the coordinate system
of their parents. Their bounds and subviews are defined in their own
coordinate system. The iPhone SDK offers several utilities that allow
you move between these coordinate systems so long as the views involved
live within the same UIWindow. To convert a point from another view
into your own coordinate system, use convertPoint: fromView:, for example:
myPoint = [myView convertPoint:somePoint fromView:otherView];
If
the original point indicated the location of some object, the new point
retains that location but gives the coordinates with respect to myView’s origin. To go the other way, use convertPoint: toView: to transform a point into another view’s coordinate system. Similarly, convertRect: toView: and convertRect: fromView: work with CGRect structures rather than CGPoint ones.