Gold Challenge: Gradient display


#1

Hi,
I am just not getting the gradient displayed. I cannot figure it out. I just copy/pasted the code from the solutions ZIP-File and it doesnt display either.
What is wrong here? I hit a serious wall.

I create the logo-views in BNRLogo.m and pass them as properties to the Appdelegate which puts one View inside the other (to be able to have shadow and masktobounds)

HypnosisView.h

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

@interface HypnosisView : UIView
{}
@property (nonatomic, strong) UIColor *circleColor;

-(UIColor *)makeCrazyColor;

@end[/code]

HypnosisView.m

[code]#import “HypnosisView.h”
#import “BNRLogo.h”
#define ARC4RANDOM_MAX 0x100000000
@implementation HypnosisView
@synthesize circleColor;
-(void)setCircleColor:(UIColor *)clr
{
circleColor = clr;
[self setNeedsDisplay];

}
-(UIColor *)makeCrazyColor
{
CGFloat redRand = ((CGFloat)arc4random()/(CGFloat)ARC4RANDOM_MAX);
CGFloat greenRand = ((CGFloat)arc4random()/(CGFloat)ARC4RANDOM_MAX);
CGFloat blueRand = ((CGFloat)arc4random()/(CGFloat)ARC4RANDOM_MAX);

// Check if all three Colors are 1






UIColor *crazyColor = [[UIColor alloc]initWithRed:redRand green:greenRand blue:blueRand alpha:1.0];
   
return crazyColor;

}

  • (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
    if (motion == UIEventSubtypeMotionShake){
    NSLog(@“Device started shaking”);
    [self setCircleColor:[UIColor redColor]];
    }
    }

  • (BOOL) canBecomeFirstResponder
    {
    return YES;
    }

-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self){
// All Hypnosisviews start with a clear background color

    [self setBackgroundColor:[UIColor clearColor]];
    [self setCircleColor:[UIColor lightGrayColor]];
}
return self;

}

-(void)drawRect:(CGRect)dirtyRect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect bounds = [self bounds];

// Figure out the center of the bounds rectangle
CGPoint center;
center.x = bounds.origin.x + bounds.size.width / 2.0;
center.y = bounds.origin.y + bounds.size.height / 2.0;

// The radius of the circle should be nearly as big as the view
float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;

// The tickness of the line should be 10 points wide
CGContextSetLineWidth(ctx, 10);

// The color of the line should be gray (RGB = 0.6, alpha = 1.0)

//[[UIColor lightGrayColor] setStroke];
//[[self circleColor] setStroke];








// Draw concentric circles from the outside in
for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20){
    // Add a path to the context
    circleColor = [self makeCrazyColor];
    NSLog(@"%@", circleColor);
    [[self circleColor] setStroke];
    CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI * 2.0, YES);
    [self makeCrazyColor];
    // Perform drawing
    CGContextStrokePath(ctx);
}

// Create a string
NSString *text = @"Your are getting sleepy";

// Get a font to draw it in
UIFont *font = [UIFont boldSystemFontOfSize:28];

CGRect textRect;

// How big is this string when drawn in this font?
textRect.size = [text sizeWithFont:font];

// Let's put that string in the center of the view
textRect.origin.x = center.x - textRect.size.width / 2.0;
textRect.origin.y = center.y - textRect.size.height / 2.0;

// set the fill color of the current context to black
[[UIColor blackColor] setFill];

// the shadow will move 4 points to the right and 3 points down from the text
CGSize offset = CGSizeMake(4, 3);

// The shadow will be dark gray in color
CGColorRef color = [[UIColor darkGrayColor] CGColor];

// Set the shadow of the context with these parameters,
// all the subsquent drawing will be shadowed
CGContextSetShadowWithColor(ctx, offset, 2.0, color);

// Draw the string
[text drawInRect:textRect
        withFont:font];

// Draw Crosshair

CGFloat crosshairSize = 50;

CGContextSaveGState(ctx);

[[UIColor greenColor] setStroke];
CGContextSetShadowWithColor(ctx, offset, 2.0, NULL);
CGContextMoveToPoint(ctx, center.x-crosshairSize/2, center.y);
CGContextAddLineToPoint(ctx, center.x+crosshairSize/2, center.y);
CGContextMoveToPoint(ctx, center.x, center.y-crosshairSize/2);
CGContextAddLineToPoint(ctx, center.x, center.y+crosshairSize/2);

CGContextStrokePath(ctx);
CGContextRestoreGState(ctx);

}
@end
[/code]

HypnoSisterAppDelegate.h

[code]#import <UIKit/UIKit.h>
#import “HypnosisView.h”

@interface HypnosisterAppDelegate : UIResponder <UIApplicationDelegate, UIScrollViewDelegate>
{
HypnosisView *view;
}
@property (strong, nonatomic) UIWindow *window;

@end
[/code]

HypnosisterAppDelegate.m

#import "HypnosisterAppDelegate.h"
#import "HypnosisView.h"
#import "BNRLogo.h"

@implementation HypnosisterAppDelegate


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

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [[UIApplication sharedApplication] setStatusBarHidden:YES
                                            withAnimation:UIStatusBarAnimationFade];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    CGRect screenRect = [[self window] bounds];
    
    // Create the UIScrollView to have the size of the window, matching its size
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
    [[self window] addSubview:scrollView];
               

    
    //HypnosisView *view = [[HypnosisView alloc] initWithFrame:[[self window] bounds]];
    
    // Create the HypnosisView with a frame that is twice the size of the screen
    CGRect bigRect = screenRect;
  
  
    view = [[HypnosisView alloc] initWithFrame:screenRect];

    [scrollView setMinimumZoomScale:1.0];
    [scrollView setMaximumZoomScale:5.0];
    
    [scrollView setDelegate:self];
       
    // Add the HypnososView as a subview of the scrollView instead of the window
    [scrollView addSubview:view];
    BNRLogo *logoView = [[BNRLogo alloc]initWithFrame:screenRect];
   
    [scrollView addSubview:[logoView containerView]];
    [[logoView containerView]addSubview:[logoView imageView]];
    

    
    
    // Tell the scrollView how big its virtual world is
    [scrollView setContentSize:bigRect.size];
    
    BOOL success = [view becomeFirstResponder];
    if (success){
        NSLog(@"Hypnosisview became the first responder");
    }
    else {
        NSLog(@"Could not become the first responder");
    }

    
    
    return YES;
}

- (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

BNRLogo.h

[code]#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface BNRLogo : UIView
@property UIImageView *imageView;
@property UIImageView *containerView;

@end
[/code]

BNRLogo.m

[code]#import “BNRLogo.h”

@implementation BNRLogo
@synthesize imageView, containerView;

  • (id)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
    UIImage *img = [UIImage imageNamed:@“Icon.png”];
    UIImageView *image = [[UIImageView alloc]initWithImage:img];
    UIImageView *container = [[UIImageView alloc]init];
    CALayer *imageLayer = image.layer;
    CALayer *containerLayer = container.layer;
    // Am Rand abscgneiden

      containerLayer.shadowColor = [UIColor blackColor].CGColor;
      containerLayer.shadowOpacity = 0.5;
      containerLayer.shadowRadius = 2;
      containerLayer.shadowOffset = CGSizeMake (10.0, 10.0);
      
      imageLayer.masksToBounds = YES;
      [imageLayer setCornerRadius:55];
      [containerLayer setCornerRadius:55];
      [imageLayer setBorderWidth:1];
      [containerLayer setBorderWidth: 1];
      
      
      // Gradients
      CGContextRef currentContext = UIGraphicsGetCurrentContext();
      CGRect bounds = [self bounds];
    
      CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
      CGFloat components[8] = {0.8, 0.8, 1, 1, 0.8, 0.8, 1, 0};
      CGFloat locs[2] = {0, 1};
      CGGradientRef gradient = CGGradientCreateWithColorComponents(space, components, locs, 2);
      CGContextDrawLinearGradient(currentContext, gradient, CGPointMake(bounds.size.width / 2.0, 0), CGPointMake(bounds.size.width / 2.0, bounds.size.height / 2.0), kCGGradientDrawsBeforeStartLocation);
      
      CGColorSpaceRelease(space);
      CGGradientRelease(gradient);
      
      
    
      
    
      
      
      
      // Property mit image füllen
      imageView = image;
      containerView = container;
      
      NSLog(@"VIEW INIT");
    

    }
    return self;
    }

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

  • (void)drawRect:(CGRect)rect
    {
    // Drawing code
    }
    */

@end
[/code]