No Lines at pg 240 build


#1

I’ve built this twice now and don’t get any lines either on the simulator or my phone.

I did some debugging and it is getting the touches events so I’m at a loss as to what is going on.

My BNRDrawView.m

[code]//
//
// BNRDrawView.m
// TouchTracker
//
// Created by John T. Shea on 3/3/14.
// Copyright © 2014 John T. Shea. All rights reserved.
//

#import “BNRDrawView.h”
#import “BNRLine.h”

@interface BNRDrawView ()

@property (nonatomic,strong) BNRLine *currentLine;
@property (nonatomic,strong) NSMutableArray *finishedLines;

@end

@implementation BNRDrawView

  • (instancetype)initWithFrame:(CGRect)r
    {
    self = [super initWithFrame:r];

    if (self) {
    self.finishedLines = [[NSMutableArray alloc]init];
    self.backgroundColor = [UIColor grayColor];
    }

    return self;
    }

  • (void)strokeLine:(BNRLine *)line
    {

    UIBezierPath *bp = [UIBezierPath bezierPath];
    bp.lineWidth = 10;
    bp.lineCapStyle = kCGLineCapRound;

    [bp moveToPoint:line.begin];
    [bp moveToPoint:line.end];
    [bp stroke];
    }

  • (void)drawRect:(CGRect)rect
    {
    // draw finished lines in black
    [[UIColor blackColor]set ];
    for (BNRLine *line in self.finishedLines) {
    [self strokeLine:line];
    }

    if (self.currentLine) {
    [[UIColor redColor]set];
    [self strokeLine:self.currentLine];
    }
    }

  • (void)touchesBegan:(NSSet *)touches
    withEvent:(UIEvent *)event
    {
    UITouch *t = [touches anyObject];

    // get location of the touch in view’s coordinate system
    CGPoint location = [t locationInView:self];

    self.currentLine = [[BNRLine alloc]init];
    self.currentLine.begin = location;
    self.currentLine.end = location;

    [self setNeedsDisplay];

}

  • (void)touchesMoved:(NSSet *)touches
    withEvent:(UIEvent *)event
    {
    UITouch *t = [touches anyObject];
    CGPoint location = [t locationInView:self];

    self.currentLine.end = location;

    [self setNeedsDisplay];

}

  • (void)touchesEnded:(NSSet *)touches
    withEvent:(UIEvent *)event
    {
    [self.finishedLines addObject:self.currentLine];

    self.currentLine = nil;

    [self setNeedsDisplay];
    }
    @end
    [/code]


#2

Hi there,

Take a close look at your drawRect method. Moving the path doesn’t show any lines.

Good luck.


#3

Turns out my problem was in strokeLine:. I had a typo, the second moveToPoint: should have been addLineToPoint: