Game Development Reference
In-Depth Information
CG_INLINE CGSize
CGSizeMake(CGFloat width, CGFloat height)
{
CGSize size; size.width = width; size.height = height; return size;
}
CG_INLINE CGRect
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
CGRect rect;
rect.origin.x = x; rect.origin.y = y;
rect.size.width = width; rect.size.height = height;
return rect;
}
Listing 4-2 shows the utility functions that can be used to create new geometry values. Only the
definition is shown, because the declaration of these functions is trivial. The type CGFloat is defined
simply as float . Presumably, this could be changed to support a different architecture. Also,
CG_INLINE is simply defined as static inline , indicating that the compiler may inject a compiled
version into the calling code.
Using Core Graphics Types
The types and functions defined by Core Graphics are pretty straightforward. Let's look at an
example to illustrate their use. Listing 4-3 shows one UIView being added to another at a specific
region.
Listing 4-3. Example of Adding a Subview and Specifying the Frame
UIView* parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)];
UIView* subView = [UIView new];
CGRect frame = CGRectMake(200, 100, 30, 40);
[subView setFrame:frame];
[parentView addSubview:subView];
In Listing 4-3, we create two UIView s in two different ways. The UIView parentView is created in
the more standard of the two ways, by calling alloc and then calling initWithFrame: and passing
in a new CGRect to specify its location and size. Another perfectly valid way to create a UIView is by
simply calling new and then setting the frame property, as we do for subView . The last thing done
in this code snippet is to add subView to parentView by calling addSubview: . The result would be
something like Figure 4-3 shown earlier.
Now that you understand the basics of the UIView subclasses and how they are nested and placed,
you can take a look at a few basic animations. This will give you the understanding required to
explore our simple game.
 
Search WWH ::




Custom Search