Bronze, Silver & Gold Challenge


#1

HypnosisterAppDelegate.m

[code]#import “HypnosisterAppDelegate.h”
#import “HypnosisView.h”
#import “fcallauView.h”

@implementation HypnosisterAppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

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

    CGRect screenRect = [[self window] bounds];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
    [scrollView setMinimumZoomScale:1.0];
    [scrollView setMaximumZoomScale:5.0];
    [scrollView setDelegate:self];
    [[self window] addSubview:scrollView];

    CGRect bigRect = screenRect;
    view = [[HypnosisView alloc] initWithFrame:screenRect];
    [scrollView addSubview:view];

    [scrollView setContentSize:bigRect.size];

    BOOL success = [view becomeFirstResponder];

    if (success)
    {
    NSLog(@“HypnosisView became the first responder”);
    }
    else
    {
    NSLog(@“Could not become first responder”);
    }

    //*********************************************************************
    // *
    // Gold Challenge *
    // *

    CGRect fcallauRect = CGRectMake(2, 2, 100, 100);
    fcallauView *fView = [[fcallauView alloc] initWithFrame:fcallauRect];
    [view addSubview:fView];

    // *
    //*********************************************************************

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

  • (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
    {
    return view;
    }

  • (void)applicationWillResignActive:(UIApplication *)application
    {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

  • (void)applicationDidEnterBackground:(UIApplication *)application
    {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

  • (void)applicationWillEnterForeground:(UIApplication *)application
    {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

  • (void)applicationDidBecomeActive:(UIApplication *)application
    {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

  • (void)applicationWillTerminate:(UIApplication *)application
    {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

@end[/code]

HypnosisView.m

[code]#import “HypnosisView.h”

@implementation HypnosisView

@synthesize circleColor;

  • (id)initWithFrame:(CGRect)frame
    {
    self=[super initWithFrame:frame];
    if (self){
    [self setBackgroundColor:[UIColor clearColor]];
    [self setCircleColor:[UIColor lightGrayColor]];
    }
    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);

    [circleColor setStroke];

    //****************************
    // *
    // Bronze Challenge *
    // *

    CGFloat componentColorRed;
    CGFloat componentColorGreen;
    CGFloat componentColorBlue;

    // *
    //****************************

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

      //*****************************************************************************************************************
      //                                                                                                                *
      //                                                Bronze Challenge                                                *
      //                                                                                                                *
      
      componentColorRed = (arc4random() % 100 + 1) / 100.00;
      componentColorGreen = (arc4random() % 100 + 1) / 100.00;
      componentColorBlue = (arc4random() % 100 + 1) / 100.00;        
      [[UIColor colorWithRed:componentColorRed green:componentColorGreen blue:componentColorBlue alpha:1.0] setStroke];
      
      //                                                                                                                *
      //*****************************************************************************************************************
      
      CGContextAddArc(ctx, center.x, center.y, currentRadius, 0, M_PI * 2, YES);
      
      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];

    //*************************
    // *
    // Silver Challenge *
    // *

    CGContextSaveGState(ctx);

    // *
    //*************************

    CGContextSetShadowWithColor(ctx, offset, 2.0, color);

    [text drawInRect:textRect withFont:font];

    //**************************************************************************
    // *
    // Silver Challenge *
    // *

    CGContextRestoreGState(ctx);

    int distance = 20;
    int wide = 2;

    CGContextMoveToPoint(ctx, center.x - wide, center.y + wide);
    CGContextAddLineToPoint(ctx, center.x - wide - distance, center.y + wide);
    CGContextAddLineToPoint(ctx, center.x - wide - distance, center.y - wide);
    CGContextAddLineToPoint(ctx, center.x - wide, center.y - wide);
    CGContextAddLineToPoint(ctx, center.x - wide, center.y - distance);
    CGContextAddLineToPoint(ctx, center.x + wide, center.y - distance);
    CGContextAddLineToPoint(ctx, center.x + wide, center.y - wide);
    CGContextAddLineToPoint(ctx, center.x + distance, center.y - wide);
    CGContextAddLineToPoint(ctx, center.x + distance, center.y + wide);
    CGContextAddLineToPoint(ctx, center.x + wide, center.y + wide);
    CGContextAddLineToPoint(ctx, center.x + wide, center.y + distance);
    CGContextAddLineToPoint(ctx, center.x - wide, center.y + distance);
    CGContextAddLineToPoint(ctx, center.x - wide, center.y + distance);
    CGContextAddLineToPoint(ctx, center.x - wide, center.y + wide);

    [[UIColor greenColor] setFill];
    CGContextFillPath(ctx);

    // *
    //**************************************************************************
    }

  • (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[/code]

fcallauView.h

[code]#import <Foundation/Foundation.h>

@interface fcallauView : UIView

@end[/code]

fcallauView.m

[code]#import “fcallauView.h”

@implementation fcallauView

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

  • (void)drawRect:(CGRect)rect
    {
    CGFloat xCenterRect = rect.origin.x + rect.size.width / 2;
    CGFloat yCenterRect = rect.origin.y + rect.size.height / 2;

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextSaveGState(ctx);

    CGContextAddArc(ctx, xCenterRect, yCenterRect, 48, 0, M_PI * 2, YES);
    CGContextClip(ctx);

    UIImage *logoBNR = [UIImage imageNamed:@“Icon@2x.png”];
    [logoBNR drawInRect:rect];

    // Gradient
    CGFloat colors[] = {0.0, 0.0, 1.0, 0.7, 1.0, 1.0, 1.0, 0.0};
    CGGradientRef fGradientRef = CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), colors, NULL, 2);
    CGPoint fStartPoint = CGPointMake(xCenterRect, 0);
    CGPoint fEndPoint = CGPointMake(xCenterRect, yCenterRect);

    CGContextDrawLinearGradient(ctx, fGradientRef, fStartPoint, fEndPoint, kCGGradientDrawsBeforeStartLocation);

    CGContextRestoreGState(ctx);

    // Shadow
    CGContextSetShadowWithColor(ctx, CGSizeMake(0, 2), 2, [[UIColor darkGrayColor] CGColor]);
    CGContextSetLineWidth(ctx, 1);
    CGContextAddArc(ctx, xCenterRect, yCenterRect, 48, 0, M_PI * 2, YES);
    CGContextStrokePath(ctx);
    }

@end[/code]