My solution for Colors challenge (simple solution)


Here is my very simple solution for the colors challenge.

It simply uses UIColor colorWithRed: (float) green: (float) blue: (float) and the optional (although recommended) srand C command.

Here is HypnosisView.h

#import <Foundation/Foundation.h>

@interface HypnosisView : UIView


Here is HypnosisView.m

#import “HypnosisView.h”

@implementation HypnosisView

//What rectangle am I filling?
CGRect bounds = [self bounds];

//Where is its center?
CGPoint center;
center.x = bounds.origin.x + bounds.size.width  * 0.5;
center.y = bounds.origin.y + bounds.size.height * 0.5;

//From the center how far out to a corner?
float maxRadius = hypot(bounds.size.width, bounds.size.height) * 0.5;

//Get the context being drawn upon
CGContextRef context = UIGraphicsGetCurrentContext();

//All lines will be drawn 10 points wide
CGContextSetLineWidth(context, 10.0f);

//Draw concetric circles from the outside in
for(float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20)
    //Create random stroke colors
    [[UIColor colorWithRed:(float)((rand() % 100) * 0.01) green:(float)((rand() % 100) * 0.01) blue:(float)((rand() % 100) * 0.01) alpha:1] setStroke];
    CGContextAddArc(context, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES);

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

//Get a font to draw it in
UIFont *font = [UIFont boldSystemFontOfSize:28];

//Where am I going to draw it?
CGRect textRect;
textRect.size = [text sizeWithFont:font];
textRect.origin.x = center.x - textRect.size.width  * 0.5;
textRect.origin.y = center.y - textRect.size.height * 0.5;

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

//Set the shadow to be offset 4 points right, 3 points down
//and dark grey with a blur radius of 2 points
CGSize offset = CGSizeMake(4, 3);
CGColorRef color = [[UIColor darkGrayColor] CGColor];
CGContextSetShadowWithColor(context, offset, 2.0, color);

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



Here is HypnosisterAppDelegate.h

#import <UIKit/UIKit.h>

//Forward Declaration
@class HypnosisView;

@interface HypnosisterAppDelegate : UIResponder <UIApplicationDelegate, UIScrollViewDelegate>
HypnosisView *view;

@property (strong, nonatomic) IBOutlet UIWindow *window;


And here is HypnosisterAppDelegate.m (notice the srand function at the top, its the only part that changed)

#import “HypnosisterAppDelegate.h”
#import “HypnosisView.h”

@implementation HypnosisterAppDelegate

@synthesize window = _window;

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    //Seed random number

    //Make a CGRect that is the size of the window
    CGRect wholeWindow = [[self window] bounds];

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:wholeWindow];
    [[self window] addSubview:scrollView];

    //Make your view twice as large as the window
    CGRect reallyBigRect;
    reallyBigRect.origin = CGPointZero;
    reallyBigRect.size.width = wholeWindow.size.width * 2.0;
    reallyBigRect.size.height = wholeWindow.size.height * 2.0;
    [scrollView setContentSize:reallyBigRect.size];

    //Center it in the scroll view
    CGPoint offset;
    offset.x = wholeWindow.size.width * 0.5;
    offset.y = wholeWindow.size.height * 0.5;
    [scrollView setContentOffset:offset];

    //Enable Zooming
    [scrollView setMinimumZoomScale:0.5];
    [scrollView setMaximumZoomScale:5];
    [scrollView setDelegate:self];

    //Create an instance of HypnosisView that is the same size as the window
    view = [[HypnosisView alloc] initWithFrame:reallyBigRect];

    //Set the background color of the viwe to be “clear”
    [view setBackgroundColor:[UIColor clearColor]];
    [scrollView addSubview:view];

    [scrollView release];

    //Hide the status bar
    [[UIApplication sharedApplication] setStatusBarHidden:YES

    [[self window] makeKeyAndVisible];
    return YES;

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
return view;

[view release];
[_window release];
[super dealloc];