BNRDrawView will keep track of all of the lines that have been drawn and the line that is currently being drawn. In BNRDrawView.m, create two instance variables in the class extension that will hold the lines in their two states. Make sure to import BNRLine.h and implement initWithFrame:.
#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; }
We will get to how lines are created in a moment, but in order to test that the line creation code is written correctly, you need the BNRDrawView to be able to draw lines.
In BNRDrawView.m, implement drawRect: to draw the current and finished lines.
- (void)strokeLine:(BNRLine *)line { UIBezierPath *bp = [UIBezierPath bezierPath]; bp.lineWidth = 10; bp.lineCapStyle = kCGLineCapRound; [bp moveToPoint:line.begin]; [bp addLineToPoint: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) { // If there is a line currently being drawn, do it in red [[UIColor redColor] set]; [self strokeLine:self.currentLine]; } }