Graphics Reference
In-Depth Information
with one currently on the screen, move the sprite position, and then draw to the swapped
out off-screen graphics context. Lather, rinse, repeat.
Although this process isn't terribly difficult, it can present some challenges to the
developer that Core Animation completely eliminates. The Core Animation way to do
the exact same thing is to create a layer that contains your sprite and simply call
-setPosition . As you learn in chapters to come, the position of a layer is represented by
a single point on the screen. By default, a layer's position is the center point of the layer
within its parent's coordinate space. So what does that mean?
Core Animation uses the standard Cartesian coordinate system you remember from geom-
etry and trigonometry. The values for x are on the horizontal axis, and the values for y are
on vertical axis. When naming a point, the x is always the first value, so a value of 25,35
means x = 25 and y = 35. Different from a standard Cartesian coordinate plane, however,
is that you are only ever using the upper-right portion of the plane where the numbers
are all positive. This means that the bottom-left corner of the screen is at point 0,0 . The
upper-right portion of the screen then is the width ( x ) and height ( y ) depending on the
resolution of the screen you use. On a 15-inch MacBook Pro, this value is 1440,900 . For
example, if you created a sprite on a layer and wanted to animate it from coordinate 0,0
on this MacBook Pro, you could set the position of the sprite to 0,0 when it is created
and then call -setPosition(CGPointMake(1440,900)) and the sprite layer would animate
to the upper-right corner of the screen. Of course, this assumes that the sprite is in a view
that covers the entire screen, as layers cannot display without being backed by a view.
Differing Coordinate Systems
One thing you should keep in mind when determining where to position your layers on a view
is that the coordinate system might work differently from one system to another. If you have
experience drawing in a coordinate system on other platforms such as Windows, the coordi-
nate system might feel upside down. On Windows, the origin point 0,0 is located at the
upper-left corner of the screen instead of the bottom-left corner.
Fortunately, if you are used to using a coordinate system that works this way, Cocoa views
provide a simple way to help you feel right at home. You can create your own NSView derived
view and override the method called -isFlipped . In this method, simply return YES (a true
value) and Quartz (the 2D drawing system) assumes you want to draw everything with your
coordinate origin in the upper-left corner.
Interestingly, this coordinate system—with the origin in the upper-left corner—is the default
for drawing behavior in a UIView on the iPhone, so keep that in mind if you need to duplicate
drawing functionality between OS X and the iPhone. You want to override NSView and have
-isFlipped return YES for OS X so that you don't need to rewrite your code for the iPhone.
Search WWH ::




Custom Search