Reduce the Frequency of Motion Updates
Users generate motion events whenever they move, shake, or tilt an iOS device. These motion events are detected by device hardware, such as the accelerometer, gyroscope, and magnetometer. An app can use this information to provide an immersive user experience. For example, a game may allow the user to move a character around the screen by tilting the device.
Stop Orientation Change Notifications When No Longer Needed
Your app can register to receive notifications when the orientation of the device changes, providing an opportunity to readjust the layout or perform other reactive actions. Make sure you disable these notifications if they become unnecessary—for example, if the user has navigated to a different view that is only available in portrait orientation. Doing so lets the system power down the accelerometer hardware if it’s not being used elsewhere.
Before registering for orientation change notifications, activate the accelerometer by calling the beginGeneratingDeviceOrientationNotifications
method for the current device object. To start notifications, send the message addObserver:selector:name:object:
to the default notification center of your app (an instance of NSNotificationCenter
). Pass it UIDeviceOrientationDidChangeNotification
and a selector to call when the device orientation changes. Then, when you no longer need to know about orientation changes, call the default notification center’s removeObserver:
method to stop the notifications. Finally, call the endGeneratingDeviceOrientationNotifications
method for the current device object to let the system know you don’t need the accelerometer anymore. See Listing 15-1.
Objective-C
-(void) viewDidLoad {
// Turn on the accelerometer
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
//Register for orientation change notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged:(NSNotification *)notification {
// Respond to changes in orientation here
}
-(void) viewDidDisappear: (BOOL) animated {
// Stop receiving orientation change notifications
[[NSNotificationCenter defaultCenter] removeObserver:self];
// Turn off the accelerometer
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}
Swift
override func viewDidLoad() {
// Turn on the accelerometer
UIDevice.currentDevice.beginGeneratingDeviceOrientationNotifications()
//Register for orientation change notifications
NSNotificationCenter.defaultCenter().addObserver(self, selector: "orientationChanged:", name: UIDeviceOrientationDidChangeNotification, object: nil)
}
func orientationChanged(notification: NSNotification!) {
// Respond to changes in orientation here
}
override func viewDidDisappear(animated: Bool) {
// Stop receiving orientation change notifications
NSNotificationCenter.defaultCenter().removeObserver(self)
// Turn off the accelerometer
UIDevice.currentDevice().endGeneratingDeviceOrientationNotifications()
}
Request Fewer Continuous Motion Updates
The Core Motion framework provides APIs that let an app receive continuous motion updates in the form of accelerometer, gyroscope, and device motion (rotation, acceleration, and more) events. Once any of these APIs is initiated, the app can request motion updates at any time. It can also register to receive recurring updates.
Before registering for recurring updates, specify an interval that meets your app’s needs. The larger the interval, the fewer events are delivered to your app, improving battery life. Call any of the methods in Table 15-1 on the Core Motion Manager class (CMMotionManager
), and pass an interval of type NSTimeInterval
.
Method |
Description |
---|---|
Call this method to set the interval for recurring accelerometer updates. |
|
Call this method to set the interval for recurring gyroscope updates. |
|
Call this method to set the interval for recurring device motion updates. |
To enable continuous motion tracking, call any of the methods in Table 15-2 on the CMMotionManager
class, depending on the requirements of your app.
Method |
Description |
---|---|
Call this method to start recording accelerometer events. To request event details at any time, query the |
|
Call this method to start recording accelerometer events and receiving updates. Pass it a processing handler to call at a recurring interval, and an operation queue on which to run the handler. |
|
Call this method to start recording gyroscope events. To request event details at any time, check the |
|
Call this method to start recording gyroscope events and receiving updates. Pass it a handler to call at a recurring interval, and an operation queue on which to run the handler. |
|
Call this method to start recording device motion events. To request event details at any time, check the |
|
Call this method to start recording device motion events and receiving updates. Pass it a handler to call at a recurring interval, and an operation queue on which to run the handler. |
When motion updates are no longer needed, make sure you explicitly stop the updates so the corresponding hardware can be powered down. Call any of the methods in Table 15-3 on the CMMotionManager
class, as appropriate.
Method |
Description |
---|---|
Call this method to stop receiving accelerometer updates. |
|
Call this method to stop receiving gyroscope updates. |
|
Call this method to stop receiving device motion updates. |
Listing 15-2 demonstrates the techniques above by registering for and stopping recurring accelerometer updates.
Objective-C
-(void) viewDidLoad {
[super viewDidLoad];
// Create a Core Motion Manager object
self.motionManager = [[CMMotionManager alloc] init];
}
- (void)startAccelerometerUpdates {
// Check whether the accelerometer is available
if ([self.motionManager isAccelerometerAvailable] == YES) {
// Update the recurring update interval
[self.motionManager setAccelerometerUpdateInterval:updateInterval];
// Start accelerometer updates
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
// Handler to process accelerometer data
}];
}
}
- (void)stopUpdates {
// Check whether the accelerometer is available
if ([self.motionManager isAccelerometerActive] == YES) {
// Start accelerometer updates
[self.motionManager stopAccelerometerUpdates];
}
}
Swift
override func viewDidLoad() {
// Create a location manager object
self.motionManager = CMMotionManager()
}
func startAccelerometerUpdates() {
// Check whether the accelerometer is available
if self.motionManager.accelerometerAvailable {
// Update the recurring update interval
self.motionManager.accelerometerUpdateInterval = updateInterval
// Start accelerometer updates
self.motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) { (accelerometerData: CMAccelerometerData!, error: NSError!) in
// Handler to process accelerometer data
}
}
}
func stopUpdates() {
// Check whether the accelerometer is available
if self.motionManager.accelerometerActive {
// Start accelerometer updates
self.motionManager.stopAccelerometerUpdates()
}
}
Reduce Location Accuracy and Duration
Copyright © 2018 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2016-09-13