Graphics Reference
In-Depth Information
LISTING 11-13
Continued
CGColorRef newBackgroundColor;
if ( CGColorEqualToColor ([ colorBar backgroundColor ], [ selectedButton myColor ])) {
newBackgroundColor = kBlackColor ;
} else {
newBackgroundColor = [ selectedButton myColor ];
}
CABasicAnimation *animation = [ CABasicAnimation
animationWithKeyPath : @”backgroundColor” ];
[animation setFromValue :( id )[ colorBar backgroundColor]];
[animation setToValue :( id )newBackgroundColor];
[animation setRemovedOnCompletion : NO ];
//[animation setDelegate : self];
[animation setAutoreverses : NO ];
[ colorBar addAnimation :animation forKey : @”colorChange” ];
[ colorBar setBackgroundColor :newBackgroundColor];
}
The changes required in the mouseUp , as shown in Listing 11-13, are the most aggressive.
First, if there was no button selected previously, we abort right away. This prevents an
accidental click if the mouse is moved over a button after the mouse button has been
pressed.
Next, remove the tracking area to stop receiving mouseEntered and mouseExited events.
Now that the mouse button is up, there is no need to further update the LZButtonLayer .
The next step is to find out if the mouse is currently over the same LZButtonLayer that
we started with. We do this by requesting the current LZButtonLayer that is under the
mouse. If that is not the same LZButtonLayer that we started with, we abort by setting
the selectedButton to nil and returning out of the method. This prevents the wrong
button from being clicked when the user presses down the mouse on one button but
releases the mouse over a different button.
After all the logic checks have passed, set the backgroundColor of the colorBar . While
doing this, first check to see if the backgroundColor is already set to the button's color. If
it is, the background is set to black instead. Otherwise we set it to the button's color.
Now when the application is run, not only does the colorBar change to the appropriate
color, but also we can cancel a click of the button by moving the mouse off the button
and then releasing.
Keyboard Events
Keyboard and mouse events are handled in a similar way. As with mouse events, only
NSResponder objects can receive keyboard events. However, unlike mouse events,
keyboard events do not have a point and are passed only to the current first responder. In
 
 
Search WWH ::




Custom Search