Text not displaying properly and circleColor unidentified


#1

I’ve been following along with the code in book, but I have run into two problems that I have absolutely no idea how to solve.

First, when I was attempting to display the text “You are getting sleepy” over the hypnosis screen, the text appeared in the upper right hand corner. There was nothing I could do to make it budge, including commenting out the relative values and simply hard-coding in logical unit values.

Second, when attempting to make the colors change when the device was shaken, I typed the code

- (void)setCircleColor:(UIColor *)clr { circleColor = clr; [self setNeedsDisplay]; }

Directly out of the book into XCode 4. I got an error that says "…unidentified identifier ‘circleColor’; did you mean ‘_circleColor’?"
If I accept XCode’s suggestion, the app doesn’t change color when shaken, despite the fact that all of the other code is in place. Has anyone else experienced anything like this?


#2

What does the drawRect: method look like?


#3

Here’s the entire block of code for drawRect:

[code]- (void)drawRect:(CGRect)dirtyRect
{

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect bounds = [self bounds];

//Figure out the center of the bounds rectangle
CGPoint center;
center.x = bounds.origin.x + bounds.size.width/2.0;
center.y = bounds.origin.y + bounds.size.height/2.0;

//The radius of the circle should be nearly as big as the view
float maxRadius = hypot(bounds.size.width, bounds.size.height)/2.0;

//The thickness of the line should be 10 points wide
CGContextSetLineWidth(ctx, 10);

[[self circleColor] setStroke];

//The color of the line should be gray (red/green/blue = 0.6, alpha = 1.0);
[[UIColor lightGrayColor] setStroke];

// Draw concentric circles from the outside in
for(float currentRadius = maxRadius; currentRadius >0; currentRadius -= 20)
{
    //Add a path to the context
    CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES);
    
    //Perform drawing instruction; remove path
    CGContextStrokePath(ctx);
}

//Create a string
NSString * text = @"Your 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.x = center.y - textRect.size.height / 2.0;

//Set the fill color of the current context to black
[[UIColor blackColor] setFill];

CGSize offset = CGSizeMake(4,3);
CGColorRef color = [[UIColor darkGrayColor] CGColor];

CGContextSetShadowWithColor(ctx, offset, 2.0, color);

//Draw the string
[text drawInRect:textRect
        withFont:font];

}[/code]


#4

NEVERMIND I FIGURED IT OUT. The @synthesize circleColor statement got deleted! Problem solved