Graphics Reference
In-Depth Information
CHAPTER 11
IN THIS CHAPTER
.
The Click of a Mouse
.
Hit Testing CALayer Objects
.
Hit Test
.
Example Application: Color
Changer
W
hat is the point of a graphical interface if a user
cannot interact with it? However, a quick exploration of
the Core Animation API reveals that it has no direct way to
receive user interaction!
.
LZButtonLayer
.
Interface Builder
.
Building the Layers
.
Watching the Mouse
This chapter focuses on how to add interaction points to
an application, specifically to Core Animation. We look at
both mouse interaction and keyboard input.
.
Keyboard Events
.
Layer-Backed Views
The most common interaction users expect from your
application is the ability to click the mouse on various
interface elements to perform some function, such as click-
ing a Save button. In a normal Cocoa application, these
type of events are handled with
NSResponder
. However,
because Core Animation was designed to be as light as
possible,
CALayer
does not inherit from
NSResponder
, and
the layer cannot accept mouse events. Instead, you need to
route the event through
NSView
.
When working with layer-backed views, your app can
capture mouse events in an
NSView
and process them there.
However, things get a little more interesting when you
work in a configuration in which there is only one
NSView
for an entire stack of
CALayer
objects. Because the
NSView
is
the only object that receives events, it must figure out what
layer was clicked on and what action to take.
When an application has only one (or at least very few)
NSView
object, all user interaction becomes the responsibil-
ity of that lone
NSView
. It receives all mouse and keyboard