In Chapter 3, you were introduced to some of the basic user interface elements of the iPhone. Many objects support high-level events such as buttonClicked
and tableRowSelected
to notify the application of certain actions taken by the user. These actions rely on lower-level mouse events provided by the UIView
class and a base class underneath it: UIResponder
. The UIResponder
class provides methods to recognize and handle the basic mouse events that occur when the user taps or drags on the iPhone's screen. These methods are incorporated into other events created in UIView
to detect two-fingered gestures. Higher-level objects, such as tables and alert sheets, take these low-level events and wrap them into even higher-level ones to handle button clicks, row selection, and other types of behavior. All such screen-oriented events are processed using the Graphics Services framework, which provides screen coordinates, fingering information, and other data related to the graphics of the event. These functions tell the application exactly what has occurred on the screen and provide the information needed to interact with the user.
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.
A CGPoint
is the simplest Core Graphics 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 Y, the vertical pixel value. These values can also be accessed directly:
float x = point.x; float y = point.y;
The iPhone's display resolution is 320Ã480 pixels. The upper-left corner of the screen is referenced at 0Ã0, and the lower right at 319Ã479 (pixel values 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 0Ã240 (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 (0Ã240). Which one it means is determined by the context where the structure is being used in the program.
Two CGPoint
structures can be compared using the CGPointEqualToPoint
function:
BOOL isEqual = CGPointEqualToPoint(point1, point2);
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. Values can be directly accessed using the structure's width
and height
variable names:
float width = size.width; float height = size.height;
Two CGSize
structures can be compared using the CGSizeEqualToSize
function:
BOOL isEqual = CGSizeEqualToSize(size1, size2);
The CGRect
structure combines 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, 200, 320, 240);
This example describes a 320Ã240 window whose upper-left corner is located at coordinates 0Ã200. 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.
The components of the CGRect
structure can also be accessed 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;
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);
A similar function can be used to determine whether one CGRect
structure contains another CGRect
structure. This is useful when testing whether certain objects overlap:
BOOL containsRect = CGRectContainsRect(rect1, rect2);
To determine whether two CGRect
structures intersect, use the CGRectIntersectsRect
function:
BOOL doesIntersect = CGRectIntersectsRect(rect1, rect2);
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:
CGRectGetMinX
Returns the coordinate of the left edge of the rectangle.
CGRectGetMinY
Returns the coordinate of the bottom edge of the rectangle.
CGRectGetMidX
Returns the center X coordinate of the rectangle.
CGRectGetMidY
Returns the center Y coordinate of the rectangle.
CGRectGetMaxX
Returns the coordinate of the right edge of the rectangle.
CGRectGetMaxY
Returns the coordinate of the upper edge of the rectangle.
Get iPhone Open Application Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.