Using Accelerometer Data

This application will use the accelerometer data to offset the center of drawing in the HypnosisView. So, HypnosisView needs two properties to define the offset. In HypnosisView.h, add these properties.

@​i​n​t​e​r​f​a​c​e​ ​H​y​p​n​o​s​i​s​V​i​e​w​ ​:​ ​U​I​V​i​e​w​
{​
 ​ ​ ​ ​f​l​o​a​t​ ​x​S​h​i​f​t​,​ ​y​S​h​i​f​t​;​
}​
@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​,​ ​a​s​s​i​g​n​)​ ​f​l​o​a​t​ ​x​S​h​i​f​t​;​
@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​,​ ​a​s​s​i​g​n​)​ ​f​l​o​a​t​ ​y​S​h​i​f​t​;​
@​e​n​d​

Now synthesize these properties in HypnosisView.m:

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​H​y​p​n​o​s​i​s​V​i​e​w​
@​s​y​n​t​h​e​s​i​z​e​ ​x​S​h​i​f​t​,​ ​y​S​h​i​f​t​;​

HypnosisView needs to know how to use these properties when it draws. In HypnosisView.m, add code to drawRect: to offset the center using the xShift and yShift instance variables:

 ​ ​ ​ ​/​/​ ​D​r​a​w​ ​c​o​n​c​e​n​t​r​i​c​ ​c​i​r​c​l​e​s​
 ​ ​ ​ ​f​o​r​ ​(​f​l​o​a​t​ ​c​u​r​r​e​n​t​R​a​d​i​u​s​ ​=​ ​m​a​x​R​a​d​i​u​s​;​ ​c​u​r​r​e​n​t​R​a​d​i​u​s​ ​>​ ​0​;​ ​c​u​r​r​e​n​t​R​a​d​i​u​s​ ​-​=​ ​2​0​)​
 ​ ​ ​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​c​e​n​t​e​r​.​x​ ​+​=​ ​x​S​h​i​f​t​;​
 ​ ​ ​ ​ ​ ​ ​ ​c​e​n​t​e​r​.​y​ ​+​=​ ​y​S​h​i​f​t​;​
 ​ ​ ​ ​ ​ ​ ​ ​C​G​C​o​n​t​e​x​t​A​d​d​A​r​c​(​c​o​n​t​e​x​t​,​ ​c​e​n​t​e​r​.​x​,​ ​c​e​n​t​e​r​.​y​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​c​u​r​r​e​n​t​R​a​d​i​u​s​,​ ​0​,​ ​M​_​P​I​ ​*​ ​2​.​0​,​ ​Y​E​S​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​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​)​;​
 ​ ​ ​ ​}​

Using the UIAcceleration object the accelerometer sends its delegate, set xShift and yShift and redraw the view. In HypnosisViewController.m, replace the following method:

-​ ​(​v​o​i​d​)​a​c​c​e​l​e​r​o​m​e​t​e​r​:​(​U​I​A​c​c​e​l​e​r​o​m​e​t​e​r​ ​*​)​m​e​t​e​r​
 ​ ​ ​ ​ ​ ​ ​ ​d​i​d​A​c​c​e​l​e​r​a​t​e​:​(​U​I​A​c​c​e​l​e​r​a​t​i​o​n​ ​*​)​a​c​c​e​l​
{​
 ​ ​ ​ ​H​y​p​n​o​s​i​s​V​i​e​w​ ​*​h​v​ ​=​ ​(​H​y​p​n​o​s​i​s​V​i​e​w​ ​*​)​[​s​e​l​f​ ​v​i​e​w​]​;​
 ​ ​ ​ ​[​h​v​ ​s​e​t​X​S​h​i​f​t​:​1​0​.​0​ ​*​ ​[​a​c​c​e​l​ ​x​]​]​;​
 ​ ​ ​ ​[​h​v​ ​s​e​t​Y​S​h​i​f​t​:​-​1​0​.​0​ ​*​ ​[​a​c​c​e​l​ ​y​]​]​;​

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

Build and run your application. The center of the view will move as you tilt the device. But the movement of the HypnosisView has a jerky feel that is not so conducive to hypnosis. We’ll fix that next.

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

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