Graphics Reference
In-Depth Information
After the header is configured, the next step is to go back to Interface Builder and
complete the bindings. You can do this by Control-dragging from the application delegate
to the window, button, and label; binding each of these to their appropriate
IBOutlet
s.
Control-drag from the Star App Delegate to
.
The background of the window, and select window from the Outlets pop-up.
.
The
UILabel
, and select
textLabel
from the Outlets pop-up.
.
The
UIButton
, and select
drawButton
from the Outlets pop-up.
When complete, Control-drag from the button back to the application's delegate and
select the
-drawStar:
Events method. This causes the
-drawStar:
method to be invoked
when the button is touched.
With the header complete it is time to begin the implementation details. Because the
configuration settings for this application were handled in Interface Builder, the only
thing that needs to be done to the
-applicationDidFinishLaunching:
(in the project's
StarStepsAppDelegate
) method is to bring the window to the front and make it the Key
Responder, as shown in Listing 13-2.
LISTING 13-2
The -applicationDidFinishLaunching: Method
- (
void
)applicationDidFinishLaunching:(UIApplication *)application
{
[window makeKeyAndVisible];
[textLabel setCenter:CGPointMake(160.0f, 100.0f)];
}
Next, we need to implement the
-drawStar:
method shown in Listing 13-3, again in the
StarStepsAppDelegate
class. This method first disables the draw button (to avoid it being
clicked multiple times during the animation) and then configures a
CGPath
for
UILabel
to
follow. Finally, it applies that path to the
UILabel
using a
CAKeyframeAnimation
.
LISTING 13-3
The -drawStar: Implementation
- (
IBAction
)drawStar:(
id
)sender;
{
NSLog(
@”%@
:
%s fired”
, [
self
class],
_cmd
);
[drawButton setEnabled:
NO
];
CGMutablePathRef starPath = CGPathCreateMutable();
CGPathMoveToPoint(starPath,
NULL
,160.0f, 100.0f);
CGPathAddLineToPoint(starPath,
NULL
, 100.0f, 280.0f);
CGPathAddLineToPoint(starPath,
NULL
, 260.0, 170.0);
CGPathAddLineToPoint(starPath,
NULL
, 60.0, 170.0);
CGPathAddLineToPoint(starPath,
NULL
, 220.0, 280.0);
CGPathCloseSubpath(starPath);