Drawing with BNRDrawView

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:.

#​i​m​p​o​r​t​ ​"​B​N​R​D​r​a​w​V​i​e​w​.​h​"​
#​i​m​p​o​r​t​ ​"​B​N​R​L​i​n​e​.​h​"​

@​i​n​t​e​r​f​a​c​e​ ​B​N​R​D​r​a​w​V​i​e​w​ ​(​)​

@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​,​ ​s​t​r​o​n​g​)​ ​B​N​R​L​i​n​e​ ​*​c​u​r​r​e​n​t​L​i​n​e​;​
@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​,​ ​s​t​r​o​n​g​)​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​f​i​n​i​s​h​e​d​L​i​n​e​s​;​

@​e​n​d​

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​B​N​R​D​r​a​w​V​i​e​w​

-​ ​(​i​n​s​t​a​n​c​e​t​y​p​e​)​i​n​i​t​W​i​t​h​F​r​a​m​e​:​(​C​G​R​e​c​t​)​r​
{​
 ​ ​ ​ ​s​e​l​f​ ​=​ ​[​s​u​p​e​r​ ​i​n​i​t​W​i​t​h​F​r​a​m​e​:​r​]​;​

 ​ ​ ​ ​i​f​ ​(​s​e​l​f​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​s​e​l​f​.​f​i​n​i​s​h​e​d​L​i​n​e​s​ ​=​ ​[​[​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​s​e​l​f​.​b​a​c​k​g​r​o​u​n​d​C​o​l​o​r​ ​=​ ​[​U​I​C​o​l​o​r​ ​g​r​a​y​C​o​l​o​r​]​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​r​e​t​u​r​n​ ​s​e​l​f​;​
}​

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.

-​ ​(​v​o​i​d​)​s​t​r​o​k​e​L​i​n​e​:​(​B​N​R​L​i​n​e​ ​*​)​l​i​n​e​
{​
 ​ ​ ​ ​U​I​B​e​z​i​e​r​P​a​t​h​ ​*​b​p​ ​=​ ​[​U​I​B​e​z​i​e​r​P​a​t​h​ ​b​e​z​i​e​r​P​a​t​h​]​;​
 ​ ​ ​ ​b​p​.​l​i​n​e​W​i​d​t​h​ ​=​ ​1​0​;​
 ​ ​ ​ ​b​p​.​l​i​n​e​C​a​p​S​t​y​l​e​ ​=​ ​k​C​G​L​i​n​e​C​a​p​R​o​u​n​d​;​

 ​ ​ ​ ​[​b​p​ ​m​o​v​e​T​o​P​o​i​n​t​:​l​i​n​e​.​b​e​g​i​n​]​;​
 ​ ​ ​ ​[​b​p​ ​a​d​d​L​i​n​e​T​o​P​o​i​n​t​:​l​i​n​e​.​e​n​d​]​;​
 ​ ​ ​ ​[​b​p​ ​s​t​r​o​k​e​]​;​
}​

-​ ​(​v​o​i​d​)​d​r​a​w​R​e​c​t​:​(​C​G​R​e​c​t​)​r​e​c​t​
{​
 ​ ​ ​ ​/​/​ ​D​r​a​w​ ​f​i​n​i​s​h​e​d​ ​l​i​n​e​s​ ​i​n​ ​b​l​a​c​k​
 ​ ​ ​ ​[​[​U​I​C​o​l​o​r​ ​b​l​a​c​k​C​o​l​o​r​]​ ​s​e​t​]​;​
 ​ ​ ​ ​f​o​r​ ​(​B​N​R​L​i​n​e​ ​*​l​i​n​e​ ​i​n​ ​s​e​l​f​.​f​i​n​i​s​h​e​d​L​i​n​e​s​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​[​s​e​l​f​ ​s​t​r​o​k​e​L​i​n​e​:​l​i​n​e​]​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​i​f​ ​(​s​e​l​f​.​c​u​r​r​e​n​t​L​i​n​e​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​I​f​ ​t​h​e​r​e​ ​i​s​ ​a​ ​l​i​n​e​ ​c​u​r​r​e​n​t​l​y​ ​b​e​i​n​g​ ​d​r​a​w​n​,​ ​d​o​ ​i​t​ ​i​n​ ​r​e​d​
 ​ ​ ​ ​ ​ ​ ​ ​[​[​U​I​C​o​l​o​r​ ​r​e​d​C​o​l​o​r​]​ ​s​e​t​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​s​e​l​f​ ​s​t​r​o​k​e​L​i​n​e​:​s​e​l​f​.​c​u​r​r​e​n​t​L​i​n​e​]​;​
 ​ ​ ​ ​}​
}​

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset