Bronze Challenge Rainbow


#1

I thought it would be fun to create a rainbow colored Hypnosis. :slight_smile:
I also would be open to any suggestions on how to “re-iterate” over an array. My version may not be the most efficient.

HypnosisView.m:
[code]//
// HypnosisView.m
// Hypnosister
//
// Created by Colby on 1/17/13.
// Copyright © 2013 Colby. All rights reserved.
//

#import “HypnosisView.h”

@implementation HypnosisView
@synthesize circleColor;

  • (id)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
    // All HypnosisViews start with a clear background color
    [self setBackgroundColor:[UIColor clearColor]];
    [self setCircleColor:[UIColor redColor]];

    }
    return self;
    }

  • (BOOL)canBecomeFirstResponder
    {
    return YES;
    }

  • (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];
    }

  • (void)drawRect:(CGRect)dirtyRec
    {
    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);

    /*************** BRONZE CHALLENGE **/
    NSArray array = [[NSArray alloc] initWithObjects:[UIColor redColor],[UIColor orangeColor],[UIColor yellowColor],[UIColor greenColor],[UIColor blueColor], [UIColor purpleColor], nil];
    int i = 0;
    // Draw concentric cirlces from the outside in
    for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) {
    [[array objectAtIndex:i]setStroke];
    // Add a path to the context
    CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES);
    // Perform drawing instructions; removes path
    CGContextStrokePath(ctx);
    i++;
    // re-iterate over the array starting back at redColor
    if (i == [array count]) {
    i = 0;
    }
    }
    /
    END BRONZE CHALLENGE ****************/

    // 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 deaw 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];
    }

@end
[/code]


#2

I would do the array count prior to the for loop and assign it to a variable:

int arrayCount = [array count];

And then in the for loop I would just use the modulus operator with the objectAtIndex:

[[array objectAtIndex:(i % arrayCount)] setStroke];

That way you can run through your array forever essentially.


#3

I did something similar but instead used a switch statement and randomly choose a color. A bonus of this was when I shook the device it would then shake up the colors.

//
//  HypnosisView.m
//  Hypnosister

#import "HypnosisView.h"

@implementation HypnosisView

@synthesize circleColor;

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

- (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];
    
    // 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; removes path
        CGContextStrokePath(ctx);
        
        // Add a random color
        switch (rand() % 5) {
            case 0:
                [[UIColor purpleColor] setStroke];
                break;
            case 1:
                [[UIColor blueColor] setStroke];
                break;
            case 2:
                [[UIColor greenColor] setStroke];
                break;
            case 3:
                [[UIColor yellowColor] setStroke];
                break;
            case 4:
                [[UIColor orangeColor] setStroke];
                break;
            default:
                break;
        }
        
    }
    
    // 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];
                
    
    
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (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