Bronze Challenge


#1

I did it with a Do-While loop; I think this is the easiest way of dealing with this prob; since we do not need to introduce any Arrays or Mutable Arrays, or uses stand() functions;

[ CGFloat red=-0.8;
CGFloat green=0.1;
CGFloat blue=-1.0;
CGFloat alpha=1;
int i=0;

for (float currentRadious=maxRadious; currentRadious>0; currentRadious-=20) {
    CGContextAddArc(ctx, center.x, center.y, currentRadious, 0.0, M_PI*2.0, YES);
    do {
        [[[UIColor alloc] initWithRed:red green:green blue:blue alpha:alpha] setStroke];
        CGContextStrokePath(ctx);
        red+=0.1;
        green+=.15;
        blue+=.2;
        
        i++;
    }while (i<2);
}][/code]

#2

I found an array to be simplest for me:

    NSArray *colors = [[NSArray alloc] initWithObjects:[UIColor purpleColor],
                       [UIColor magentaColor], [UIColor yellowColor], [UIColor redColor],
                       [UIColor blueColor], [UIColor greenColor], nil];
    
    for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) {
        [[colors objectAtIndex:rand() % [colors count]] setStroke];
        CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES);
        CGContextStrokePath(ctx);
    }

#3

I went with a method. Very very random colors.

- (UIColor *)randomColor
{
    CGFloat red = (arc4random() % 101) / 100.0;
    CGFloat green = (arc4random() % 101) / 100.0;
    CGFloat blue = (arc4random() % 101) / 100.0;
    CGFloat alpha = (arc4random() % 101) / 100.0;
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

And in the for loop, just


#4

I solved the Bronze challenge but with a problem, the shake gesture no longer turns my circles red. When I comment out the portion of the program which draws different circles, the shake gesture does work, I’ve studied the code and can’t seem to figure this out.

[code]#import “HypnosisView.h”

@implementation HypnosisView
@synthesize circleColor;

  • (BOOL)canBecomeFirstResponder
    {
    return YES;
    }

  • (id)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {

      [self setBackgroundColor:[UIColor clearColor]];
    

    }
    return self;
    }

  • (void)drawRect:(CGRect)dirtyRect

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

CGPoint center;
center.x = bounds.origin.x + bounds.size.width / 2.0;
center.y = bounds.origin.y + bounds.size.height / 2.0;

 
float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;


CGContextSetLineWidth(ctx, 10);

int(i) = 0;
int (x) = 0;

    

for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) {

    
    CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI *2, YES);
    
    if (x == 0) {
    
    [self setCircleColor:[UIColor redColor]];
 }
if (x == 1) {
    [self setCircleColor:[UIColor greenColor]];
 }

if (x == 2) {
[self setCircleColor:[UIColor blueColor]];
}

    i ++;
x = i % 3;

    [[self circleColor] setStroke];
    
    CGContextStrokePath(ctx);
  

    
}


NSString *text = @"You are getting sleepy.";

UIFont *font = [UIFont boldSystemFontOfSize:28];

CGRect textRect;

textRect.size = [text sizeWithFont:font];

textRect.origin.x = center.x - textRect.size.width / 2.0;
textRect.origin.y = center.y - textRect.size.height / 2.0;

[[UIColor blackColor] setFill];

CGSize offset = CGSizeMake(4, 3);

CGColorRef color = [[UIColor darkGrayColor] CGColor];

CGContextSetShadowWithColor(ctx, offset, 2.0, color);

    [text drawInRect:textRect
        withFont:font];

}

  • (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {

    if (motion == UIEventSubtypeMotionShake) {
    NSLog(@“Device started shaking”);
    [self setCircleColor:[UIColor redColor]];
    }
    }

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

@end
[/code]


#5

[quote=“macintux”]I found an array to be simplest for me:

[code]
NSArray *colors = [[NSArray alloc] initWithObjects:[UIColor purpleColor],
[UIColor magentaColor], [UIColor yellowColor], [UIColor redColor],
[UIColor blueColor], [UIColor greenColor], nil];

for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) {
    [[colors objectAtIndex:rand() % [colors count]] setStroke];
    CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES);
    CGContextStrokePath(ctx);
}

[/code][/quote]

I found this was the best way to do it.


#6

[quote=“jkdorman”]I solved the Bronze challenge but with a problem, the shake gesture no longer turns my circles red. When I comment out the portion of the program which draws different circles, the shake gesture does work, I’ve studied the code and can’t seem to figure this out.
[/quote]

Figured this question out after understanding how/when drawRect is being called. Essentially, when you perform a shake command you end up calling setCircleColor which then calls the setNeedsDisplay. This in turns calls drawRect which contains your color pattern for the circles. This overwrites the red coloring of the circles back into your predefined pattern.