touchesBegan failed, NSlog not print and no color change


#1

I just programed following the chapter 5 of the book, but when I use the simulator, no color changed and no content print when I touch the screen.

Xcode 7.2 IOS 9.2

AppDelegate.m

#import "AppDelegate.h"
#import "BNRHypnosisView.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;
    [code][/code]
    BNRHypnosisView *firstView = [[BNRHypnosisView alloc] initWithFrame:firstFrame];
    
//    firstView.backgroundColor = [UIColor redColor];
    
    [self.window addSubview:firstView];
    
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [[UIViewController alloc]init];
    [self.window makeKeyAndVisible];
    return YES;
}

BNRHypnosisView.m

#import "BNRHypnosisView.h"

@interface BNRHypnosisView ()

@property (strong, nonatomic) UIColor *circleColor;

@end

@implementation BNRHypnosisView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //设置BNRHypnosisView对象的背景颜色为透明
        self.backgroundColor = [UIColor clearColor];
        self.circleColor = [UIColor lightGrayColor];
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%@ was touched", self);
    
    //获取3个0到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;
}

- (void)setCircleColor:(UIColor *)circleColor
{
    _circleColor = circleColor;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGRect bounds = self.bounds;
    
    //根据bounds计算中心点
    CGPoint center;
    center.x = bounds.origin.x + bounds.size.width / 2.0;
    center.y = bounds.origin.y + bounds.size.height / 2.0;
    
    //根据视图宽和高中的较小值计算圆形的半径
//    float radius = (MIN(bounds.size.width, bounds.size.height) / 2.0);
    
    //使最外层圆形成为视图的外接圆
    float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
    
    UIBezierPath *path = [[UIBezierPath alloc] init];
    
    //以中心点为圆心,radius的值为半径,定义一个0到M_PI * 2.0弧度的路径(整圆)
//    [path addArcWithCenter:center radius:radius startAngle:0.0 endAngle:M_PI * 2.0 clockwise:YES];
    
    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];
    }
    
    //设置线条宽度为10点;
    path.lineWidth = 10;
    
//    [[UIColor lightGrayColor] setStroke];
    [self.circleColor setStroke];
    
    //绘制路径
    [path stroke];
}

#2

把这一行:
[self.window addSubview:firstView];
改为:
[self.window.rootViewController.view addSubview:firstView];
就可以了。


#3

got it, really thank you.------------------------------------------------------------------发件人:Philip.Z bignerdranch@discoursemail.com发送时间:2016年11月17日(星期四) 15:44收件人:liyiming0722 liyiming0722@aliyun.com主 题:[Big Nerd Ranch Book Forums] [iOS Programming: The Big Nerd Ranch Guide (4th Edi/5. Views: Redrawing and UIScrollView] touchesBegan failed, NSlog not print and no color change

          Philip.Z
          

          November 17
        
      
    
  



  把这一行:

[self.window addSubview:firstView];
改为:
[self.window.rootViewController.view addSubview:firstView];
就可以了。

Visit Topic or reply to this email to respond.
To unsubscribe from these emails, click here.


#4

感谢!能解释一下背后的逻辑吗?是跟ViewController有关吗?