Help, Cannot make circle change to red


#1

Im following the example and I checked all the code and seems to be ok.

The method setCircleColor is being executed but the color is not being changed.

Here is my code:

#import "HypnosisView.h"


@implementation HypnosisView

@synthesize circleColor;

- (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) / 4.0;
    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);
//    CGContextSetRGBStrokeColor(ctx, 0.6, 0.6, 0.6, 1.0);
    [[UIColor lightGrayColor]setStroke];
    
    // Add a shape to the context - this does not draw the shape
//    CGContextAddArc(ctx, center.x, center.y, maxRadius, 0.0, M_PI * 2.0, YES);
    
    // Perform a drawing instruction; draw current shape with current state
//    CGContextStrokePath(ctx);
    
    // 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, YES);
        
        // Perform drawing instruction; removes path
        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];
    
    // The shadow will move 4 points to the right and 3 points down from the text
    CGSize offset = CGSizeMake(4, 3);
    
    // The shadow will be dark gray in color
    CGColorRef color = [[UIColor darkGrayColor] CGColor];
    
    // Set the shadow of the context with these parameters
    // all subsequent drawing will be shadowed
    CGContextSetShadowWithColor(ctx, offset, 2.0, color);
    
    // Draw the string
    [text drawInRect:textRect withFont:font];
    
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        // All HypnosisView start with a clear background color
        [self setBackgroundColor:[UIColor clearColor]];
        [self setCircleColor:[UIColor lightGrayColor]];
    }
    return self;
}


- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake){
        NSLog(@"Device started shaking!");
        [self setCircleColor:[UIColor blueColor]];
    }
}

-(void)setCircleColor:(UIColor *)clr
{
    NSLog(@"Changing color");
    circleColor = clr;
    [self setNeedsDisplay];
    NSLog(@"Circle color changed");
    
}

@end

I added some flags on the method to know if it is being executed and it is but the color is never changed.
What am I doing wrong?

thanks.


#2

I found my error.

I never replaced the line

[[UIColor lightGrayColor]setStroke];

and that’s why the circle never changed.
duh!
I guess is part of the programming experience.


#3

So true! The only caveat I’ve found is that one needs must search the code for errors with diligence.

Interestingly, I had the exact same problem and arrived at the exact same solution.

In so doing, one observes exactly how the code is working (to the best of one’s understanding) and can make debugging experiments. Is that method being called? Insert an NSLog statement into the suspect method and check the db console for the message to verify that the method is, indeed being called.

In my case, anyway, I need to be careful not to ask for help before I’ve fulfilled my duty of carefully examining the code. There’s a certain resistance to stretching one’s brain, and that’s exactly the resistance that has to be overcome in order to progress. Same goes for using the API documentation (again, at least in my case). There is an odd pleasure in defeating this intellectual inertia, especially when one achieves that which one intended.

Things I’ve learned:

  1. Do one thing at a time.
  2. Save versions or snapshots.
  3. Patience, patience, patience.
  4. Likewise perseverance. (Don’t forget to breathe.)
  5. Do ask.
  6. Failure is the hallmark of learning. It’s temporary-- at least until next time.

:slight_smile: Regards.


#4

Hey guys

I had the same problem. Like you say codefish, it is a good feeling when you go through the error finding process and finally work it out.

As far as I could see the book didn’t clearly indicate that we should delete that line of text on p139 so it was easy to miss.

Cheers