Silver 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”

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

@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”

@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];

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

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

@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]