No color change

I feel like I’ve put all the code in as the book says but I don’t get the initial color change… what am I doing wrong?

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

@interface BNRAppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;

@end[/code]

[code]#import “BNRAppDelegate.h”
#import “BNRHypnosisView.h”

@implementation BNRAppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    CGRect firstFrame = self.window.bounds;
    BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];
    [self.window addSubview:firstView];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    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
[/code]

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

@interface BNRHypnosisView : UIView

@end[/code]

[code]#import “BNRHypnosisView.h”
@interface BNRHypnosisView()
@property (strong, nonatomic) UIColor *circleColor;
@end
@implementation BNRHypnosisView

  • (void)drawRect:(CGRect)rect
    {
    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 largest circle will circumscribe the view
    float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;

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

    for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -=20)
    {
    [path moveToPoint:CGPointMake(center.x + currentRadius, center.y)];
    [path addArcWithCenter:center radius:currentRadius startAngle:0.0 endAngle:M_PI * 2.0 clockwise:YES];
    }

    //Configure line width to 10 points
    path.lineWidth = 10;

    [self.circleColor setStroke];
    [path stroke];

}

  • (instancetype) initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if(self){
    //All BNRHypnosisViews start with a clear backgroudn color
    self.backgroundColor = [UIColor clearColor];
    self.circleColor = [UIColor lightGrayColor];
    }
    return self;
    }

  • (void) setcircleColor: (UIColor *) circleColor
    {
    _circleColor = circleColor;
    [self setNeedsDisplay];
    NSLog(@“set circle color method is being used”);
    }

//When a finger touches the screen

  • (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    NSLog(@"%@ was touched ", self);

    // get 3 random numbers between 0 and 1
    float red = (arc4random() %100) / 100.0;
    float green = (arc4random() %100) / 100.0;
    float blue = (arc4random() %100) / 100.0;

    UIColor *randomColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
    self.circleColor = randomColor;
    }

@end
[/code]

I just changed circleColor to backgroundColor and it changed… it worked.

#import "BNRHypnosisView.h"
@interface BNRHypnosisView()
@property (strong, nonatomic) UIColor *circleColor;
@end
@implementation BNRHypnosisView


- (void)drawRect:(CGRect)rect
{
    NSLog(@"in loop part 1");
    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 largest circle will circumscribe the view
    float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
    
    UIBezierPath *path =[[UIBezierPath alloc] init];
    
    for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -=20)
    {
        [path moveToPoint:CGPointMake(center.x + currentRadius, center.y)];
        [path addArcWithCenter:center radius:currentRadius startAngle:0.0 endAngle:M_PI * 2.0 clockwise:YES];
    }
    
    //Configure line width to 10 points
    path.lineWidth = 10;
    
    [self.circleColor setStroke];
    [path stroke];
    NSLog(@"in loop part 2");
    
}

- (instancetype) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        //All BNRHypnosisViews start with a clear backgroudn color
        self.backgroundColor = [UIColor clearColor];
        self.circleColor = [UIColor lightGrayColor];
    }
    return self;
}

- (void) setcircleColor: (UIColor *) circleColor
{
    _circleColor = circleColor;
    [self setNeedsDisplay];
    NSLog(@"set circle color method is being used");
}


//When a finger touches the screen
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%@ was touched ", self);
    
    // get 3 random numbers between 0 and 1
    float red = (arc4random() %100) / 100.0;
    float green = (arc4random() %100) / 100.0;
    float blue = (arc4random() %100) / 100.0;
    
    UIColor *randomColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
    NSLog(@" random color is  %@", randomColor);
    self.backgroundColor = randomColor;
}


@end

hello,
The same problem I met too.
When the Simulator run, the NSlog in touchesBegan method not print and color not change
have you solved this problem?

Hello,
Add a UIViewController’s subclass. touchesBegan will run.

 Code following:

BNRHypnosisViewController.h

#import <UIKit/UIKit.h>

@interface BNRHypnosisViewController : UIViewController

@end

BNRHypnosisViewController.m

#import "BNRHypnosisViewController.h"
#import "BNRHypnosisView.h"

@interface BNRHypnosisViewController ()

@end

@implementation BNRHypnosisViewController

- (void)loadView
{
    //创建一个BNRHypnosisView对象
    BNRHypnosisView *backgroundView = [[BNRHypnosisView alloc] init];
    
    //将BNRHypnosisView对象赋给视图控制器的view属性
    self.view = backgroundView;
}

@end

appDelegate.m

#import "AppDelegate.h"
#import "BNRHypnosisView.h"
#import "BNRHypnosisViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
//    CGRect firstFrame = CGRectMake(160, 240, 100, 150);
    CGRect firstFrame = self.window.bounds;
    
    BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];
    
//    firstView.backgroundColor = [UIColor redColor];
    
    [self.window addSubview:firstView];
    
    NSBundle *appBundles = [[NSBundle alloc] init];
    
    BNRHypnosisViewController *rvc = [[BNRHypnosisViewController alloc] initWithNibName:@"BNRHypnosisView"
                                                                                 bundle:appBundles];
    self.window.rootViewController = rvc;
    
    self.window.backgroundColor = [UIColor whiteColor];
//    self.window.rootViewController = [[UIViewController alloc]init];
    [self.window makeKeyAndVisible];
    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