All Challenges


#1

Hi there!

I’ll only include the .m folder that matters here. Comments are welcomed.

Heres my code:

[code]#import “BNRHypnosister.h”

@implementation BNRHypnosister

  • (id)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
    // Initialization code
    self.backgroundColor = [UIColor clearColor];
    }
    return self;
    }

  • (void)drawRect:(CGRect)rect
    {
    // Drawing code
    CGRect bounds = self.bounds;

    //Figure out the certer of the 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;

    UIBezierPath *path = [[UIBezierPath alloc] init];

    for (int currenRadius = maxRadius; currenRadius > 0; currenRadius -= 20) {

      [path moveToPoint:CGPointMake(currenRadius + center.x, center.y)];
      
      [path addArcWithCenter:center
                      radius:currenRadius
                  startAngle:0
                    endAngle:M_PI *2
                   clockwise:YES];
    

    }

    path.lineWidth = 10;

    [[UIColor lightGrayColor] setStroke];

    [path stroke];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextSetShadow(context, CGSizeMake(4, 7), 3);

    UIImage *image = [UIImage imageNamed:@“logo”];

    float ratioWidthHeignt = image.size.width / image.size.height;

    CGSize imageSize;

    if (bounds.size.width > bounds.size.height) {
    imageSize = CGSizeMake(bounds.size.height * ratioWidthHeignt, bounds.size.height);
    } else {
    imageSize = CGSizeMake(bounds.size.width, bounds.size.width * ratioWidthHeignt);
    }

    CGRect imageRect= CGRectMake(center.x - (imageSize.width / 2.0),
    center.y - (imageSize.height / 2.0),
    imageSize.width,
    imageSize.height);

    [image drawInRect:imageRect];

    CGContextRestoreGState(context);

    CGContextSaveGState(context);

    UIBezierPath *trianglePath = [[UIBezierPath alloc] init];

    [trianglePath moveToPoint:CGPointMake(center.x, center.y - bounds.size.height / 4)];
    [trianglePath addLineToPoint:CGPointMake(center.x / 2, center.y + bounds.size.height /4)];
    [trianglePath addLineToPoint:CGPointMake(center.x * 1.5, center.y + bounds.size.height /4)];
    [trianglePath closePath];
    [trianglePath addClip];

    CGFloat locations[2] = {0.0, 1.0};

    CGFloat components[8] = { 1.0, 0.0, 0.0, 1.0,
    1.0, 1.0, 0.0, 0.0};

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, 2);

    CGContextDrawLinearGradient(context,
    gradient,
    CGPointMake(center.x, center.y - bounds.size.height / 2),
    CGPointMake(center.x * 1.5, center.y + bounds.size.height /2),
    0);

    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);

    CGContextRestoreGState(context);

}

@end[/code]