My Gold Challenge Approach


#1

Hi everyone,

This has been a real challenge for me, I tried for almost 2 days (yeah embarrassing) and now I’m stuck 'cause NOT ANYMORE!! I just figure it out… really, I finally did it.

[size=200]Gold Challenge Solution[/size]

[size=150]Header File[/size]

[code]#import <Foundation/Foundation.h>
#import “MRCHypnosisView.h”

@interface MRCHypnosisterBNRLogo : MRCHypnosisView

@property (strong, nonatomic) UIImage *bnrLogo;

@end[/code]

[size=150]Implementation File[/size]

[code]#import “MRCHypnosisterBNRLogo.h”

@implementation MRCHypnosisterBNRLogo
@synthesize bnrLogo;

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

  • (void)drawRect:(CGRect)circleLogo
    {
    /********************************************************************************************************/
    //Create the CGRect struct and find its center

    //Pick a pointer to the current context (struct)
    CGContextRef contextRef = UIGraphicsGetCurrentContext();

    //Define the bounds of the frame for the logo
    CGRect logoBNR = [self bounds];

    //Define the center point of the rectangle
    CGPoint rectCenter;
    rectCenter.x = logoBNR.size.width / 2.0;
    rectCenter.y = logoBNR.size.height / 2.0;

    /********************************************************************************************************/
    //Define the parameters for the circle, its stroke line, its shadow and add the image to the frame

    //Define the radius for the circle
    float circleRadius = hypotf(logoBNR.size.width, logoBNR.size.height) / 3.0;

    //Set the color for the circle line
    [[UIColor blackColor] setStroke];

    //Set the thickness of the line
    CGContextSetLineWidth(contextRef, 0.5);

    //Create an offset for shadow
    CGSize shadowOffset = CGSizeMake(0, 1);

    //Save current state for further drawing
    CGContextSaveGState(contextRef);

    //Set a pointer to a Path struct
    CGMutablePathRef circlePath = CGPathCreateMutable();

    //Add an arc path to the circlePath
    CGPathAddArc(circlePath, NULL, rectCenter.x, rectCenter.y, circleRadius, 0.0, M_PI * 2.0, YES);

    //Set the circlePath to the current path in the context
    CGContextAddPath(contextRef, circlePath);

    //Clip the circle path
    CGContextClip(contextRef);

    //Get the image and put it inside of the frame
    bnrLogo = [UIImage imageNamed:@“BNRLogo.jpeg”];
    [bnrLogo drawInRect:logoBNR];

    /********************************************************************************************************/
    //Create the blue gradient and apply it to the frame

    //Create a space color ref
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceCMYK();

    //Create an array of colors and cast each one of them to CGColor
    NSArray *colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0.1 green:0.2 blue:0.9 alpha:0.4] CGColor],(id)[[UIColor colorWithRed:0.0 green:0.0 blue:0.1 alpha:0.0] CGColor],nil];

    //Create a pointer to a CGGradient struct with the space color and the array just created
    CGGradientRef gradientRef = CGGradientCreateWithColors(colorSpace, (CFArrayRef)colors, NULL);

    //Set the starting point for the gradient
    CGPoint gradientStart;
    gradientStart.x = rectCenter.x;
    gradientStart.y = logoBNR.origin.y;

    //Set the ending point for the gradient
    CGPoint gradientEnd;
    gradientEnd.x = rectCenter.x;
    gradientEnd.y = rectCenter.y;

    //Draw the gradient
    CGContextDrawLinearGradient(contextRef, gradientRef, gradientStart, gradientEnd, kCGGradientDrawsBeforeStartLocation);

    /********************************************************************************************************/
    //Draw the circle and add the shadow to the logo

    CGContextRestoreGState(contextRef);

    //Trace a path to a circle
    CGContextAddPath(contextRef, circlePath);

    //Apply a shadow to the circle
    CGContextSetShadowWithColor(contextRef, shadowOffset, 2.0, [[UIColor darkTextColor] CGColor]);

    //Make lighter images prevail over the darkest, (to keep the shadow just in the bottom)
    CGContextSetBlendMode(contextRef, kCGBlendModeLighten);

    //Draw the circle
    CGContextStrokePath(contextRef);
    [/code]

[size=200]The output[/size]