Creating the TouchTracker Application

Now let’s get started with your application. In Xcode, create a new Window-based Application iPhone project and name it TouchTracker.

First, you will need a model object that describes a line. Create a new NSObject and name it Line. In Line.h, declare two CGPoint instance variables and two properties:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

@​i​n​t​e​r​f​a​c​e​ ​L​i​n​e​ ​:​ ​N​S​O​b​j​e​c​t​ ​{​
 ​ ​ ​ ​C​G​P​o​i​n​t​ ​b​e​g​i​n​;​
 ​ ​ ​ ​C​G​P​o​i​n​t​ ​e​n​d​;​
}​
@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​)​ ​C​G​P​o​i​n​t​ ​b​e​g​i​n​;​
@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​)​ ​C​G​P​o​i​n​t​ ​e​n​d​;​
@​e​n​d​

In Line.m, synthesize the properties:

#​i​m​p​o​r​t​ ​"​L​i​n​e​.​h​"​

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​L​i​n​e​

@​s​y​n​t​h​e​s​i​z​e​ ​b​e​g​i​n​,​ ​e​n​d​;​

@​e​n​d​

Next, create a new NSObject called TouchDrawView. In TouchDrawView.h, change the superclass to UIView. Also, declare two collection objects: an array to hold complete lines and a dictionary to hold lines that are still being drawn. We’ll talk about why we use two different collection objects when you write the code to use them.

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​
#​i​m​p​o​r​t​ ​<​U​I​K​i​t​/​U​I​K​i​t​.​h​>​

@​i​n​t​e​r​f​a​c​e​ ​T​o​u​c​h​D​r​a​w​V​i​e​w​ ​:​ ​U​I​V​i​e​w​
{​
 ​ ​ ​ ​N​S​M​u​t​a​b​l​e​D​i​c​t​i​o​n​a​r​y​ ​*​l​i​n​e​s​I​n​P​r​o​c​e​s​s​;​
 ​ ​ ​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​c​o​m​p​l​e​t​e​L​i​n​e​s​;​
}​
-​ ​(​v​o​i​d​)​c​l​e​a​r​A​l​l​;​
@​e​n​d​

In your Hypnosister project, you instantiated your custom view programmatically. In TouchTracker, you will instantiate a custom view from a XIB file. Open MainWindow.xib.

From the object library, drag an instance of UIView onto the window. In the identity inspector, set its class to TouchDrawView, as shown in Figure 20.2.

Figure 20.2  Identity inspector

Identity inspector

Views created programmatically are sent initWithFrame:, views that are unarchived from a XIB file are sent initWithCoder:. Thus, for TouchDrawView, you will override initWithCoder: instead of initWithFrame:.

In TouchDrawView.m, take care of the creation and destruction of the two collections:

#​i​m​p​o​r​t​ ​"​T​o​u​c​h​D​r​a​w​V​i​e​w​.​h​"​
#​i​m​p​o​r​t​ ​"​L​i​n​e​.​h​"​

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​T​o​u​c​h​D​r​a​w​V​i​e​w​

-​ ​(​i​d​)​i​n​i​t​W​i​t​h​C​o​d​e​r​:​(​N​S​C​o​d​e​r​ ​*​)​c​
{​
 ​ ​ ​ ​s​e​l​f​ ​=​ ​[​s​u​p​e​r​ ​i​n​i​t​W​i​t​h​C​o​d​e​r​:​c​]​;​

 ​ ​ ​ ​i​f​ ​(​s​e​l​f​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​l​i​n​e​s​I​n​P​r​o​c​e​s​s​ ​=​ ​[​[​N​S​M​u​t​a​b​l​e​D​i​c​t​i​o​n​a​r​y​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​D​o​n​'​t​ ​l​e​t​ ​t​h​e​ ​a​u​t​o​c​o​m​p​l​e​t​e​ ​f​o​o​l​ ​y​o​u​ ​o​n​ ​t​h​e​ ​n​e​x​t​ ​l​i​n​e​,​
 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​m​a​k​e​ ​s​u​r​e​ ​y​o​u​ ​a​r​e​ ​i​n​s​t​a​n​t​i​a​t​i​n​g​ ​a​n​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​
 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​a​n​d​ ​n​o​t​ ​a​n​ ​N​S​M​u​t​a​b​l​e​D​i​c​t​i​o​n​a​r​y​!​
 ​ ​ ​ ​ ​ ​ ​ ​c​o​m​p​l​e​t​e​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​ ​s​e​t​M​u​l​t​i​p​l​e​T​o​u​c​h​E​n​a​b​l​e​d​:​Y​E​S​]​;​
 ​ ​ ​ ​}​

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

Notice that you explicitly enabled multi-touch events by sending the message setMultipleTouchEnabled:. Without this, only one touch at a time can be active on a view. If another finger touches the view, it will be ignored, and the view will not be sent touchesBegan:withEvent: or any of the other UIResponder messages.

In the dealloc method, release the objects you created.

-​ ​(​v​o​i​d​)​d​e​a​l​l​o​c​
{​
 ​ ​ ​ ​[​l​i​n​e​s​I​n​P​r​o​c​e​s​s​ ​r​e​l​e​a​s​e​]​;​
 ​ ​ ​ ​[​c​o​m​p​l​e​t​e​L​i​n​e​s​ ​r​e​l​e​a​s​e​]​;​
 ​ ​ ​ ​[​s​u​p​e​r​ ​d​e​a​l​l​o​c​]​;​
}​

Now override the drawRect: method to create lines using functions from Core Graphics:

-​ ​(​v​o​i​d​)​d​r​a​w​R​e​c​t​:​(​C​G​R​e​c​t​)​r​e​c​t​
{​
 ​ ​ ​ ​C​G​C​o​n​t​e​x​t​R​e​f​ ​c​o​n​t​e​x​t​ ​=​ ​U​I​G​r​a​p​h​i​c​s​G​e​t​C​u​r​r​e​n​t​C​o​n​t​e​x​t​(​)​;​
 ​ ​ ​ ​C​G​C​o​n​t​e​x​t​S​e​t​L​i​n​e​W​i​d​t​h​(​c​o​n​t​e​x​t​,​ ​1​0​.​0​)​;​
 ​ ​ ​ ​C​G​C​o​n​t​e​x​t​S​e​t​L​i​n​e​C​a​p​(​c​o​n​t​e​x​t​,​ ​k​C​G​L​i​n​e​C​a​p​R​o​u​n​d​)​;​

 ​ ​ ​ ​/​/​ ​D​r​a​w​ ​c​o​m​p​l​e​t​e​ ​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​ ​(​L​i​n​e​ ​*​l​i​n​e​ ​i​n​ ​c​o​m​p​l​e​t​e​L​i​n​e​s​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​C​G​C​o​n​t​e​x​t​M​o​v​e​T​o​P​o​i​n​t​(​c​o​n​t​e​x​t​,​ ​[​l​i​n​e​ ​b​e​g​i​n​]​.​x​,​ ​[​l​i​n​e​ ​b​e​g​i​n​]​.​y​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​C​G​C​o​n​t​e​x​t​A​d​d​L​i​n​e​T​o​P​o​i​n​t​(​c​o​n​t​e​x​t​,​ ​[​l​i​n​e​ ​e​n​d​]​.​x​,​ ​[​l​i​n​e​ ​e​n​d​]​.​y​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​C​G​C​o​n​t​e​x​t​S​t​r​o​k​e​P​a​t​h​(​c​o​n​t​e​x​t​)​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​/​/​ ​D​r​a​w​ ​l​i​n​e​s​ ​i​n​ ​p​r​o​c​e​s​s​ ​i​n​ ​r​e​d​
 ​ ​ ​ ​[​[​U​I​C​o​l​o​r​ ​r​e​d​C​o​l​o​r​]​ ​s​e​t​]​;​
 ​ ​ ​ ​f​o​r​ ​(​N​S​V​a​l​u​e​ ​*​v​ ​i​n​ ​l​i​n​e​s​I​n​P​r​o​c​e​s​s​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​L​i​n​e​ ​*​l​i​n​e​ ​=​ ​[​l​i​n​e​s​I​n​P​r​o​c​e​s​s​ ​o​b​j​e​c​t​F​o​r​K​e​y​:​v​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​C​G​C​o​n​t​e​x​t​M​o​v​e​T​o​P​o​i​n​t​(​c​o​n​t​e​x​t​,​ ​[​l​i​n​e​ ​b​e​g​i​n​]​.​x​,​ ​[​l​i​n​e​ ​b​e​g​i​n​]​.​y​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​C​G​C​o​n​t​e​x​t​A​d​d​L​i​n​e​T​o​P​o​i​n​t​(​c​o​n​t​e​x​t​,​ ​[​l​i​n​e​ ​e​n​d​]​.​x​,​ ​[​l​i​n​e​ ​e​n​d​]​.​y​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​C​G​C​o​n​t​e​x​t​S​t​r​o​k​e​P​a​t​h​(​c​o​n​t​e​x​t​)​;​
 ​ ​ ​ ​}​
}​

Finally, write a method that will clear the collections and redraw the view in TouchDrawView.m.

-​ ​(​v​o​i​d​)​c​l​e​a​r​A​l​l​
{​
 ​ ​ ​ ​/​/​ ​C​l​e​a​r​ ​t​h​e​ ​c​o​l​l​e​c​t​i​o​n​s​
 ​ ​ ​ ​[​l​i​n​e​s​I​n​P​r​o​c​e​s​s​ ​r​e​m​o​v​e​A​l​l​O​b​j​e​c​t​s​]​;​
 ​ ​ ​ ​[​c​o​m​p​l​e​t​e​L​i​n​e​s​ ​r​e​m​o​v​e​A​l​l​O​b​j​e​c​t​s​]​;​

 ​ ​ ​ ​/​/​ ​R​e​d​r​a​w​
 ​ ​ ​ ​[​s​e​l​f​ ​s​e​t​N​e​e​d​s​D​i​s​p​l​a​y​]​;​
}​

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

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