8
The Accelerometer

One of the flashiest features of iOS is the accelerometer. The accelerometer detects the device’s real-world orientation by tracking the force of the earth’s gravity on its X, Y, and Z axes. You can also use accelerometer data to detect changes in the device’s velocity.

In this chapter, you are going to use the accelerometer to skew the center of the HypnosisView according to the device’s orientation: when the user tilts the phone, the center will slide in the direction of the tilt, as shown in Figure 8.1.

Figure 8.1  HypnoTime, tilted

HypnoTime, tilted

Setting Up the Accelerometer

To receive accelerometer data, your application needs to get hold of the application’s shared instance of UIAccelerometer and give it an updateInterval and a delegate. The delegate needs to implement the method accelerometer:didAccelerate:. This method reports changes in the accelerometer data every updateInterval seconds in the form of a UIAcceleration object.

Open the HypnoTime project. Before you add any code, you need to decide which object will be the UIAccelerometer delegate. There are two options:

  • Make the HypnosisView the delegate. It will receive the updates directly and use them to change the center of drawing internally.
  • Make the HypnosisViewController the delegate. It will receive the updates and forward the necessary bits in messages to its view, the HypnosisView.

If the HypnosisView is the accelerometer delegate, it becomes a self-contained object, which makes reusing it simpler. The problem is there can only be one accelerometer delegate. What if other objects need input from the accelerometer? HypnosisView can’t forward information on to other objects – it’s not a controller. Therefore, the better option is to let HypnosisViewController be the delegate and receive the accelerometer updates, as shown in Figure 8.2. HypnosisViewController can easily inform the HypnosisView of a change in orientation, and it can inform other objects, too, if necessary.

Figure 8.2  Object diagram for HypnoTime

Object diagram for HypnoTime

In HypnosisViewController.h, let the HypnosisViewController know it conforms to the UIAccelerometerDelegate protocol.

@​i​n​t​e​r​f​a​c​e​ ​H​y​p​n​o​s​i​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​:​ ​U​I​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​<​U​I​A​c​c​e​l​e​r​o​m​e​t​e​r​D​e​l​e​g​a​t​e​>​

In HypnosisViewController.m, override the method viewWillAppear: to get a pointer to the accelerometer and set its update interval and delegate.

-​ ​(​v​o​i​d​)​v​i​e​w​W​i​l​l​A​p​p​e​a​r​:​(​B​O​O​L​)​a​n​i​m​a​t​e​d​
{​
 ​ ​ ​ ​[​s​u​p​e​r​ ​v​i​e​w​W​i​l​l​A​p​p​e​a​r​:​a​n​i​m​a​t​e​d​]​;​

 ​ ​ ​ ​N​S​L​o​g​(​@​"​M​o​n​i​t​o​r​i​n​g​ ​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​ ​*​a​ ​=​ ​[​U​I​A​c​c​e​l​e​r​o​m​e​t​e​r​ ​s​h​a​r​e​d​A​c​c​e​l​e​r​o​m​e​t​e​r​]​;​

 ​ ​ ​ ​/​/​ ​R​e​c​e​i​v​e​ ​u​p​d​a​t​e​s​ ​e​v​e​r​y​ ​1​/​1​0​t​h​ ​o​f​ ​a​ ​s​e​c​o​n​d​.​
 ​ ​ ​ ​[​a​ ​s​e​t​U​p​d​a​t​e​I​n​t​e​r​v​a​l​:​0​.​1​]​;​
 ​ ​ ​ ​[​a​ ​s​e​t​D​e​l​e​g​a​t​e​:​s​e​l​f​]​;​
}​

When the HypnosisViewController’s view is moved off screen, the accelerometer updates become unnecessary, and you should set the accelerometer’s delegate to nil. Setting the UIAccelerometer delegate to nil stops the updates to the controller and powers down the accelerometer hardware to conserve battery life. Make this change in HypnosisViewController.m:

-​ ​(​v​o​i​d​)​v​i​e​w​W​i​l​l​D​i​s​a​p​p​e​a​r​:​(​B​O​O​L​)​a​n​i​m​a​t​e​d​
{​
 ​ ​ ​ ​[​s​u​p​e​r​ ​v​i​e​w​W​i​l​l​D​i​s​a​p​p​e​a​r​:​a​n​i​m​a​t​e​d​]​;​
 ​ ​ ​ ​[​[​U​I​A​c​c​e​l​e​r​o​m​e​t​e​r​ ​s​h​a​r​e​d​A​c​c​e​l​e​r​o​m​e​t​e​r​]​ ​s​e​t​D​e​l​e​g​a​t​e​:​n​i​l​]​;​
}​

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

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