Golden Challenge Solution


#1

[code]//
// HypnosisterAppDelegate.h
// Hypnosister
//
// Created by Roy Law on 13-5-29.
// Copyright © 2013年 Roy Law. All rights reserved.
//

#import <UIKit/UIKit.h>
#import “HypnosisterView.h”
#import “LogoView.h”

@interface HypnosisterAppDelegate : UIResponder <UIApplicationDelegate , UIScrollViewDelegate>
{
HypnosisterView *aView;
LogoView *anotherView;
}

@property (strong, nonatomic) UIWindow *window;

@end
[/code]

[code]//
// HypnosisterAppDelegate.m
// Hypnosister
//
// Created by Roy Law on 13-5-29.
// Copyright © 2013年 Roy Law. All rights reserved.
//

#import “HypnosisterAppDelegate.h”
#import “HypnosisterView.h”
#import “LogoView.h”

@implementation HypnosisterAppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];

    UIScrollView *aScrollView = [[UIScrollView alloc] initWithFrame:self.window.bounds];

    aView = [[HypnosisterView alloc] initWithFrame:self.window.bounds];

    [aScrollView addSubview:aView];
    [aScrollView setContentSize:self.window.bounds.size];
    [aScrollView setMinimumZoomScale:1.0];
    [aScrollView setMaximumZoomScale:5.0];

    [aScrollView setDelegate:self];
    [self.window addSubview:aScrollView];

    CGRect iconRect = self.window.bounds;
    iconRect.size.width = 114;
    iconRect.size.height =114;
    iconRect.origin.x+=3;
    iconRect.origin.y+=3;
    anotherView = [[LogoView alloc]initWithFrame];
    [aView addSubview:anotherView];
    anotherView.backgroundColor = [UIColor clearColor];

    [aView becomeFirstResponder];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
    }

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return aView;
}

@end
[/code]

[code]//
// LogoView.h
// Hypnosister
//
// Created by Roy Law on 13-5-30.
// Copyright © 2013年 Roy Law. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface LogoView : UIView

@end
[/code]

[code]//
// LogoView.m
// Hypnosister
//
// Created by Roy Law on 13-5-30.
// Copyright © 2013年 Roy Law. All rights reserved.
//

#import “LogoView.h”

@implementation LogoView

-(void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();

CGRect bounds = self.bounds;

CGPoint aCenter;
aCenter.x = bounds.origin.x + bounds.size.width/2.0;
aCenter.y = bounds.origin.y + bounds.size.height/2.0;
float maxRadius = hypot(aCenter.x, aCenter.y)/1.5;

CGContextSaveGState(ctx);

CGSize offset = CGSizeMake(2, 2);
CGColorRef color = [[UIColor darkGrayColor] CGColor];
CGContextSetShadowWithColor(ctx, offset, 2.0, color);

CGContextSetLineWidth(ctx, 2);
[[UIColor blackColor] setStroke];
CGContextAddArc(ctx, aCenter.x, aCenter.y, maxRadius, 0.0, M_PI*2.0, YES);
CGContextStrokePath(ctx);

CGContextRestoreGState(ctx);

CGContextAddArc(ctx, aCenter.x, aCenter.y, maxRadius, 0.0, M_PI*2.0, YES);
CGContextClip(ctx);

UIImage *aIcon = [UIImage imageNamed:@"Icon@2x.png"];
[aIcon drawInRect:bounds];

/* create gradient */
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

// up color
UIColor *upColor = [UIColor blueColor];
const CGFloat *upColorComponents = CGColorGetComponents(upColor.CGColor);

// down color
UIColor *downColor = [UIColor clearColor];
const CGFloat *downColorComponents = CGColorGetComponents(downColor.CGColor);

const CGFloat colorComponents[8] = {
    // up color
    upColorComponents[0],
    upColorComponents[1],
    upColorComponents[2],
    upColorComponents[3],
    // down color
    downColorComponents[0],
    downColorComponents[1],
    downColorComponents[2],
    downColorComponents[3]
    };

const CGFloat colorIndices[2] = {
    0.0f,  1.0f
};

// num of colors
const unsigned int numColors = 2;

// create gradient
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace,
                                                             colorComponents,
                                                             colorIndices,
                                                             numColors);
/* draw gradient */
CGPoint startPoint = bounds.origin, endPoint = bounds.origin;
endPoint.y+=bounds.size.height/2;

int options = 0;
options = kCGGradientDrawsAfterEndLocation | kCGGradientDrawsBeforeStartLocation;

CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, options);

/* release */
CGColorSpaceRelease(colorSpace);

}

@end
[/code]

[code]//
// HypnosisterView.h
// Hypnosister
//
// Created by Roy Law on 13-5-29.
// Copyright © 2013年 Roy Law. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface HypnosisterView : UIView

@property (strong,nonatomic) UIColor *circleColor;

@end
[/code]

[code]//
// HypnosisterView.m
// Hypnosister
//
// Created by Roy Law on 13-5-29.
// Copyright © 2013年 Roy Law. All rights reserved.
//

#import “HypnosisterView.h”

@implementation HypnosisterView

@synthesize circleColor;

-(void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect bounds = self.bounds;

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

float maxRadius = hypot(aCenter.x, aCenter.y)/2.0;

CGContextSetLineWidth(ctx, 5);

// CGContextSetRGBStrokeColor(ctx, 0.6, 0.6, 0.6, 1.0);
// [[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0] setStroke];
[self.circleColor setStroke];
for (float currentRadius = maxRadius; currentRadius>0; currentRadius-=10) {
CGContextAddArc(ctx, aCenter.x, aCenter.y, currentRadius, 0.0, M_PI*2.0, YES);
CGContextStrokePath(ctx);
}

// CGContextSaveGState(ctx);

NSString *text = @"You are getting sleepy.";
UIFont *font = [UIFont boldSystemFontOfSize:28];

CGRect textRect;
textRect.size = [text sizeWithFont:font];
textRect.origin.x = aCenter.x - textRect.size.width/2.0;
textRect.origin.y = aCenter.y - textRect.size.height/2.0;

[[UIColor blackColor] setFill];

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

CGContextSetShadowWithColor(ctx, offset, 2.0, color);

[text drawInRect:textRect withFont:font];
/*
CGContextRestoreGState(ctx);

[[UIColor blueColor] setStroke];
CGContextMoveToPoint(ctx, aCenter.x, aCenter.y-100);
CGContextAddLineToPoint(ctx, aCenter.x, aCenter.y+100);
CGContextStrokePath(ctx);
CGContextMoveToPoint(ctx, aCenter.x-100, aCenter.y);
CGContextAddLineToPoint(ctx, aCenter.x+100, aCenter.y);
CGContextStrokePath(ctx);    

*/
}

-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
self.circleColor = [UIColor lightGrayColor];
}
return self;
}

-(BOOL)canBecomeFirstResponder
{
return YES;
}

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@“Device is shaking”);
[self setCircleColor:[UIColor redColor]];
}

-(void)setCircleColor:(UIColor *)cColor
{
circleColor = cColor;
[self setNeedsDisplay];
}
@end
[/code]