4.1. Introduction to Geometric Structures

Before diving into events management, you'll need a basic understanding of some geometric structures commonly used on the iPhone. You've already seen some of these in Chapter 3. The Core Graphics framework provides many general structures to handle graphics-related functions. Among these structures are points, window sizes, and window regions. Core Graphics also provides many C-based functions for creating and comparing these structures.

4.1.1. CGPoint

A CGPoint is the simplest CoreGraphics structure, and contains two floating-point values corresponding to horizontal (x) and vertical (y) coordinates on a display. To create a CGPoint, use the CGPointMake method:

CGPoint point = CGPointMake (320.0, 480.0);

The first value represents x, the horizontal pixel value, and the second represents y, the vertical pixel value. You can also access these values directly:

float x = point.x;
float y = point.y;

The iPhone's display resolution is 320x480 pixels. The upper-left corner of the screen is referenced at 0x0, and the lower-right at 319x479 (pixel offsets are zero-indexed).

Being a general-purpose structure, a CGPoint can refer equally well to a coordinate on the screen or within a window. For example, if a window is drawn at 0x240 (halfway down the screen), a CGPoint with values (0, 0) could address either the upper-left corner of the screen or the upper-left corner of the window (0x240). Which one it means is determined by the context where the structure is being used in the program.

You can compare two CGPoint structures using the CGPointEqualToPoint function:

BOOL isEqual = CGPointEqualToPoint(point1, point2);

4.1.2. CGSize

A CGSize structure represents the size of a rectangle. It encapsulates the width and height of an object and is primarily found in the iPhone APIs to dictate the size of screen objects—namely windows. To create a CGSize object, use CGSizeMake:

CGSize size = CGSizeMake(320.0, 480.0);

The values provided to CGSizeMake indicate the width and height of the element being described. You can directly access values using the structure's width and height variable names:

float width = size.width;
float height = size.height;

You can compare two CGSize structures using the CGSizeEqualToSize function:

BOOL isEqual = CGSizeEqualToSize(size1, size2);

4.1.3. CGRect

The CGRect structure combines both a CGPoint and CGSize structure to describe the frame of a window on the screen. The frame includes an origin, which represents the location of the upper-left corner of the window, and the size of the window. To create a CGRect, use the CGRectMake function:

CGRect rect = CGRectMake(0.0, 200.0, 320.0, 240.0);

This example describes a 320x240 window whose upper-left corner is located at coordinates 0x200. As with the CGPoint structure, these coordinates could reference a point on the screen itself or offsets within an existing window; it depends on where and how the CGRect structure is used.

You can also access the components of the CGRect structure directly:

CGPoint windowOrigin = rect.origin;
float x = rect.origin.x;
float y = rect.origin.y;

CGSize windowSize = rect.size;
float width = rect.size.width;
float height = rect.size.height; Containment and intersection

Two CGRect structures can be compared using the CGRectEqualToRect function:

BOOL isEqual = CGRectEqualToRect(rect1, rect2);

To determine whether a given point is contained inside a CGRect, use the CGRectContainsPoint method. This is particularly useful when determining whether a user has tapped inside a particular region. The point is represented as a CGPoint structure:

BOOL containsPoint = CGRectContainsPoint(rect, point);

You can use a similar function to determine whether one CGRect structure contains another CGRect structure. This is useful when testing whether certain objects overlap:

BOOL containsRect = CGRectContainsRect(rect1, rect2);

For one structure to contain another, all the pixels in one structure must also be in the other. In contrast, two structures intersect as long as they share at least one pixel. To determine whether two CGRect structures intersect, use the CGRectIntersectsRect function:

BOOL doesIntersect = CGRectIntersectsRect(rect1, rect2); Edge and center detection

The following functions can be used to determine the various edges of a rectangle and calculate the coordinates of the rectangle's center. All of these functions accept a CGRect structure as their only argument and return a float value:


Returns the coordinate of the left edge of the rectangle.


Returns the coordinate of the bottom edge of the rectangle.


Returns the center x coordinate of the rectangle.


Returns the center y coordinate of the rectangle.


Returns the coordinate of the right edge of the rectangle.


Returns the coordinate of the upper edge of the rectangle.