Graphics Programs Reference
In-Depth Information
UIKit Drawing Additions
There are a number of Foundation and UIKit classes that can work with a CGContex-
tRef . One such class is UIColor . An instance of UIColor represents a color and can
be used to set the current drawing color of the context. In HypnosisView.m , replace the
line of code that changes the stroke color with one that uses UIColor 's color-
WithRed:green:blue:alpha: method.
CGContextSetLineWidth(ctx, 10);
CGContextSetRGBStrokeColor(ctx, 0.6, 0.6, 0.6, 1.0);
[[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1] setStroke];
Build and run the application again. The HypnosisView will look the same as before.
There are a number of prepared UIColor instances you can use for common colors.
Change this line of code again:
[[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1] setStroke];
[[UIColor lightGrayColor] setStroke];
Build and run again. The color of the circles should be about the same, but the code is
much simpler.
NSString has the ability to draw to a CGContextRef . Sending the message
drawInRect:withFont: to an NSString will draw that string in the given rectangle
with the given font to the current context. In HypnosisView.m , add the following code
to the end of drawRect: .
for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20)
{
CGContextAddArc(ctx, center.x, center.y,
currentRadius, 0, M_PI * 2.0, YES);
CGContextStrokePath(ctx);
}
// Create a string
NSString *text = @"You are getting sleepy.";
// Get a font to draw it in
UIFont *font = [UIFont boldSystemFontOfSize:28];
CGRect textRect;
// How big is this string when drawn in this font?
textRect.size = [text sizeWithFont:font];
// Let's put that string in the center of the view
textRect.origin.x = center.x - textRect.size.width / 2.0;
textRect.origin.y = center.y - textRect.size.height / 2.0;
// Set the fill color of the current context to black
[[UIColor blackColor] setFill];
 
Search WWH ::




Custom Search