704 best practices with core motion 05 d - apple inc. · apple watch apple watch series 1 apple...

205
#WWDC17 © 2017 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. John Blackwell, Core Motion Engineer Ahmad Bleik, Core Motion Engineer Creating Immersive Apps with Core Motion Session 704 System Frameworks

Upload: others

Post on 29-Oct-2019

64 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

#WWDC17

© 2017 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

John Blackwell, Core Motion Engineer Ahmad Bleik, Core Motion Engineer

•Creating Immersive Apps with •Core Motion • Session 704

System Frameworks

Page 2: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

•Overview •Authorization •Historical Accelerometer •DeviceMotion •Badger with Attitude

Page 3: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

•Overview •Authorization •Historical Accelerometer •DeviceMotion •Badger with Attitude

Page 4: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Core Motion At a glance

CoreMotion framework Application

Accelerometer

Gyroscope

Altimeter

Magnetometer

Page 5: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Motion Interfaces

Page 6: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Motion Interfaces

CMMotionManager

Page 7: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Motion Interfaces

CMMotionManager

CMAltimeter

Page 8: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Motion Interfaces

CMMotionManager

CMAltimeter

CMPedometer

Page 9: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Motion Interfaces

CMMotionManager

CMAltimeter

CMPedometer

CMMotionActivityManager

Page 10: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Motion Interfaces

CMMotionManager

CMAltimeter

CMPedometer

CMMotionActivityManager

CMSensorRecorder

Page 11: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

•Overview •Authorization •Historical Accelerometer •DeviceMotion •Badger with Attitude

Page 12: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Sensitive Interfaces

CMAltimeter

CMPedometer

CMMotionActivityManager

CMSensorRecorder

Page 13: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Authorization

Sensitive API causes prompt

Page 14: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Authorization

Sensitive API causes prompt

Appears only once

Page 15: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch
Page 16: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Authorization Check let pedometer = CMPedometer() let now = Date()

pedometer.queryPedometerData(from:now, to:now) { (data, error) in if let code = error?._code { if code == CMErrorMotionActivityNotAuthorized.rawValue {

// Ask the user for authorization! } } }

Page 17: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Authorization Check let pedometer = CMPedometer() let now = Date()

pedometer.queryPedometerData(from:now, to:now) { (data, error) in if let code = error?._code { if code == CMErrorMotionActivityNotAuthorized.rawValue {

// Ask the user for authorization! } } }

Page 18: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Authorization Check let pedometer = CMPedometer() let now = Date()

pedometer.queryPedometerData(from:now, to:now) { (data, error) in if let code = error?._code { if code == CMErrorMotionActivityNotAuthorized.rawValue {

// Ask the user for authorization! } } }

Page 19: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Authorization Check let pedometer = CMPedometer() let now = Date()

pedometer.queryPedometerData(from:now, to:now) { (data, error) in if let code = error?._code { if code == CMErrorMotionActivityNotAuthorized.rawValue {

// Ask the user for authorization! } } }

Page 20: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Authorization Status

// CMAltimeter, CMPedometer, CMMotionActivityManager, CMSensorRecorder

@available(iOS 11.0, *) @available(watchOS 4.0, *) open class func authorizationStatus() -> CMAuthorizationStatus

NEW

Page 21: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Authorization Status

@available(iOS 11.0, *) @available(watchOS 4.0, *) public enum CMAuthorizationStatus : Int { case notDetermined case restricted case denied case authorized }

NEW

Page 22: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Authorization Check

// Best Practice: Check availability first! if CMPedometer.isStepCountingAvailable() {

switch CMPedometer.authorizationStatus() { case .notDetermined: // Handle state before user prompt break case .restricted: // Handle system-wide restriction break case .denied: // Handle user denied state break case .authorized: // Ready to go! break } }

Page 23: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Authorization Check

// Best Practice: Check availability first! if CMPedometer.isStepCountingAvailable() {

switch CMPedometer.authorizationStatus() { case .notDetermined: // Handle state before user prompt break case .restricted: // Handle system-wide restriction break case .denied: // Handle user denied state break case .authorized: // Ready to go! break } }

Page 24: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Authorization Check

// Best Practice: Check availability first! if CMPedometer.isStepCountingAvailable() {

switch CMPedometer.authorizationStatus() { case .notDetermined: // Handle state before user prompt break case .restricted: // Handle system-wide restriction break case .denied: // Handle user denied state break case .authorized: // Ready to go! break } }

Page 25: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

•Overview •Authorization •Historical Accelerometer •DeviceMotion •Badger with Attitude

Page 26: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Historical Accelerometer CMSensorRecorder

Records 50Hz accelerometer in the background

Page 27: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Historical Accelerometer CMSensorRecorder

Records 50Hz accelerometer in the background

Request up to 36 hours

Page 28: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Historical Accelerometer CMSensorRecorder

Records 50Hz accelerometer in the background

Request up to 36 hours

Stored for up to three days

Page 29: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Available

Apple Watch

Apple Watch Series 1

Apple Watch Series 2

Historical Accelerometer Availability

Page 30: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Available

Apple Watch

Apple Watch Series 1

Apple Watch Series 2

iPhone 7 and 7 Plus (on iOS 11)

Historical Accelerometer Availability

Page 31: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

App Inspiration Automotive Performance Tracker

Page 32: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

App Inspiration Automotive Performance Tracker

Use Motion Activity for automotive periods

Page 33: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Use Motion Activity for automotive periods

Automotive detection improved in iOS 11

App Inspiration Automotive Performance Tracker

Page 34: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

App Inspiration Automotive Performance Tracker

Use Motion Activity for automotive periods

Automotive detection improved in iOS 11

Use Sensor Recorder for performance data

Page 35: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

App Inspiration Automotive Performance Tracker

Use Motion Activity for automotive periods

Automotive detection improved in iOS 11

Use Sensor Recorder for performance data

Low-power, all-day experience

Page 36: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Historical Accelerometer Best practices

Choose the minimum duration

Page 37: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Historical Accelerometer Best practices

Choose the minimum duration

Decimate if possible

Page 38: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

•Overview •Authorization •Historical Accelerometer •DeviceMotion •Badger with Attitude

Page 39: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Sensors What goes into DeviceMotion?

Page 40: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Accelerometer • Acceleration from user and gravity

Sensors What goes into DeviceMotion?

Page 41: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Sensors What goes into DeviceMotion?

Accelerometer • Acceleration from user and gravity Gyroscope • Rotation rate

Page 42: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Sensors What goes into DeviceMotion?

Accelerometer • Acceleration from user and gravity Gyroscope • Rotation rate Magnetometer • Local fields and Earth’s field

Page 43: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Sensors Challenges

Page 44: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Sensors Challenges

Accelerometer • Distinguishing user vs. gravity

Page 45: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Sensors Challenges

Accelerometer • Distinguishing user vs. gravity Gyroscope • Bias over time

Page 46: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Sensors Challenges

Accelerometer • Distinguishing user vs. gravity Gyroscope • Bias over time Magnetometer • Distinguishing local vs. Earth

Page 47: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

DeviceMotion

Page 48: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

DeviceMotion

3D attitude during motion

Page 49: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

DeviceMotion

3D attitude during motion

Fuses accelerometer, gyroscope, and magnetometer

Page 50: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

DeviceMotion

3D attitude during motion

Fuses accelerometer, gyroscope, and magnetometer

Allows you to focus on the app

Page 51: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

What’s New in Core Motion WWDC 2011

Understanding Core Motion WWDC 2012

Health And Fitness With Core Motion WWDC 2016

DeviceMotion

More references

Page 52: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Reference Frames

Accelerometer and Gyroscope Magnetometer

xArbitraryZVertical

xArbitraryCorrectedZVertical

xMagneticNorthZVertical

xTrueNorthZVertical

Page 53: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Accelerometer and Gyroscope Magnetometer

xArbitraryZVertical

xArbitraryCorrectedZVertical

xMagneticNorthZVertical

xTrueNorthZVertical

Reference Frames

Page 54: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Accelerometer and Gyroscope Magnetometer

xArbitraryZVertical

xArbitraryCorrectedZVertical

xMagneticNorthZVertical

xTrueNorthZVertical

Reference Frames

Page 55: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch
Page 56: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Tilt left and right to steer

Game Control Accelerometer

Page 57: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Tilt left and right to steer

Game Control Accelerometer

Page 58: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Game Control Accelerometer

Tilt left and right to steer

Estimate gravity from accelerometer

Page 59: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Game Control Accelerometer

Tilt left and right to steer

Estimate gravity from accelerometer

Determine tilt from gravity

Page 60: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Acceleration Ambiguity

Gestures can be ambiguous

Page 61: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Acceleration Ambiguity

Gestures can be ambiguous

Page 62: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Acceleration Ambiguity

Gestures can be ambiguous

Page 63: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Acceleration Ambiguity

Gestures can be ambiguous

Could isolate gravity by averaging

Page 64: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Acceleration Ambiguity

Gestures can be ambiguous

Could isolate gravity by averaging

Filtering affects responsiveness

Page 65: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Acceleration Ambiguity

Gestures can be ambiguous

Could isolate gravity by averaging

Filtering affects responsiveness

DeviceMotion means less filtering

Page 66: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

xArbitraryZVertical

Default reference frame

Page 67: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

xArbitraryZVertical

Default reference frame

Great for tip and tilt

Page 68: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

xArbitraryZVertical

Default reference frame

Great for tip and tilt

Accelerometer and gyroscope fused

Page 69: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

xArbitraryZVertical

Default reference frame

Great for tip and tilt

Accelerometer and gyroscope fused

Gravity for tilt

Page 70: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

xArbitraryZVertical

Default reference frame

Great for tip and tilt

Accelerometer and gyroscope fused

Gravity for tilt

Demo a bit later!

Page 71: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Great for gestures

Check out SwingWatch

xArbitraryZVertical

Health and Fitness with Core Motion WWDC 2016

Page 72: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch
Page 73: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch
Page 74: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch
Page 75: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Game Control

Attitude for aiming

Page 76: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Game Control

Attitude for aiming

Attitude provides rotation from reference frame

Page 77: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Game Control

Attitude for aiming

Attitude provides rotation from reference frame

Avoid taking integral of raw gyroscope

Page 78: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

xArbitraryCorrectedZVertical

Uses magnetometer to improve horizontal attitude

Page 79: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

xArbitraryCorrectedZVertical

Uses magnetometer to improve horizontal attitude

Reliable attitude

Page 80: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

xArbitraryCorrectedZVertical

Uses magnetometer to improve horizontal attitude

Reliable attitude

Provides fixed center reference

Page 81: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch
Page 82: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch
Page 83: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Magnetometer

Provides world reference

Page 84: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Magnetometer

Provides world reference

Raw magnetometer susceptible to disturbances

Page 85: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Magnetometer

Provides world reference

Raw magnetometer susceptible to disturbances • Within the device

Page 86: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Magnetometer

Provides world reference

Raw magnetometer susceptible to disturbances • Within the device • Outside the device

Page 87: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

xMagneticNorthZVertical and xTrueNorthZVertical

Orients device to the world

Page 88: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

xMagneticNorthZVertical and xTrueNorthZVertical

Orients device to the world

Handles: • Device level effects

Page 89: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

xMagneticNorthZVertical and xTrueNorthZVertical

Orients device to the world

Handles: • Device level effects • Challenging magnetometer situations

Page 90: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

xMagneticNorthZVertical and xTrueNorthZVertical

Orients device to the world

Handles: • Device level effects • Challenging magnetometer situations

Pick frame based on your app needs

Page 91: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

App Inspiration xMagneticNorth and xTrueNorth

Page 92: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

App Inspiration xMagneticNorth and xTrueNorth

Star gazing apps

Page 93: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

App Inspiration xMagneticNorth and xTrueNorth

Star gazing apps

Augmented Reality apps

Page 94: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

App Inspiration xMagneticNorth and xTrueNorth

Star gazing apps

Augmented Reality apps

Check out ARKit

Introducing ARKit: Augmented Reality for iOS Hall 3 Tuesday 5:10PM

Page 95: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch
Page 96: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Heading

Direction with respect to north

0°Heading:

N

Page 97: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Heading

Direction with respect to north

36°Heading:

N

Page 98: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Heading

Direction with respect to north

72°Heading:

N

Page 99: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Heading

Direction with respect to north

Could use CoreLocation

72°Heading:

N

Page 100: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Heading

Direction with respect to north

Could use CoreLocation

CoreLocation’s heading can fuse course

72°Heading:

N

What's New in Location Technologies Grand Ballroom B Thursday 3:10PM

Page 101: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Heading

Direction with respect to north

Could use CoreLocation

CoreLocation’s heading can fuse course

Could calculate from attitude

72°Heading:

N

Page 102: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Direction with respect to NorthCould use CoreLocation

Fused with course Could calculate from attitudeWe now provide heading

Heading

Direction with respect to north

Could use CoreLocation

CoreLocation’s heading can fuse course

Could calculate from attitude

DeviceMotion now provides heading

72°Heading:

N

Page 103: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Heading

Fuses accelerometer, gyroscope, and magnetometer

NEW

Page 104: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Heading

Fuses accelerometer, gyroscope, and magnetometer

iOS only

NEW

Page 105: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Heading

Valid for XMagneticNorth, XTrueNorth

// CMDeviceMotion

@available(iOS 11.0, *) open var heading: Double { get }

NEW

Page 106: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Heading

Valid for XMagneticNorth, XTrueNorth

0-359 degrees from X axis (North)

// CMDeviceMotion

@available(iOS 11.0, *) open var heading: Double { get }

NEW

Page 107: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch
Page 108: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

DeviceMotion Best practices

Check for availability

Page 109: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

DeviceMotion Best practices

Check for availability

@available(iOS 5.0, *) open class func availableAttitudeReferenceFrames() -> CMAttitudeReferenceFrame

Page 110: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

DeviceMotion Best practices

Check for availability

Reference frame choice is key

Page 111: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

DeviceMotion Best practices

Check for availability

Reference frame choice is key • Attitude definition

Page 112: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

DeviceMotion Best practices

Check for availability

Reference frame choice is key • Attitude definition • Sensors used

Page 113: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

•Overview •Authorization •Historical Accelerometer •DeviceMotion •Badger with Attitude

Page 114: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Ahmad Bleik, Core Motion Engineer

•Badger with Attitude

Page 115: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger

Page 116: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger

Page 117: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger with Attitude

Badger controls

Getting started with DeviceMotion

Gesture detection

Page 118: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger Controls Swipe gestures

Page 119: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger Controls Swipe gestures

Page 120: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger Controls Swipe gestures

Page 121: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger Controls Swipe gestures

Page 122: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger Controls Motion gestures

Page 123: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger Controls Motion gestures

Page 124: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger Controls Motion gestures

Page 125: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger Controls Motion gestures

Page 126: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger Controls Motion gestures

Page 127: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger with Attitude

Page 128: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger with Attitude

DeviceMotion

Page 129: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger with Attitude

DeviceMotion• Sensor fusion

Page 130: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger with Attitude

DeviceMotion• Sensor fusion

Query mechanisms

Page 131: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger with Attitude

DeviceMotion• Sensor fusion

Query mechanisms• Push

Page 132: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Badger with Attitude

DeviceMotion• Sensor fusion

Query mechanisms• Push• Pull

Page 133: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

DeviceMotion Push

Page 134: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Detecting discrete gestures over time

DeviceMotion Push

Page 135: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Detecting discrete gestures over time

Get data at a fixed interval

DeviceMotion Push

Page 136: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Detecting discrete gestures over time

Get data at a fixed interval

DeviceMotion Push

// Push func startDeviceMotionUpdates(using referenceFrame: CMAttitudeReferenceFrame, to queue: OperationQueue, withHandler handler: CoreMotion.CMDeviceMotionHandler)

Page 137: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

DeviceMotion Pull

Page 138: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Current device state

DeviceMotion Pull

Page 139: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Current device state

Responsive

DeviceMotion Pull

Page 140: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Current device state

Responsive

DeviceMotion Pull

// Pull func startDeviceMotionUpdates(using referenceFrame: CMAttitudeReferenceFrame)

Page 141: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Using DeviceMotion import CoreMotion let motionManager = CMMotionManager()

// Before starting game logic guard motionManager.isDeviceMotionAvailable else {

print("Device Motion is not available.") return }

let myFrame = CMAttitudeReferenceFrame.xArbitraryZVertical guard CMMotionManager.availableAttitudeReferenceFrames().contains(myFrame) else { print("The reference frame XArbitraryZVertical is not available.") return}

Page 142: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Using DeviceMotion import CoreMotion let motionManager = CMMotionManager()

// Before starting game logic guard motionManager.isDeviceMotionAvailable else {

print("Device Motion is not available.") return }

let myFrame = CMAttitudeReferenceFrame.xArbitraryZVertical guard CMMotionManager.availableAttitudeReferenceFrames().contains(myFrame) else { print("The reference frame XArbitraryZVertical is not available.") return}

Page 143: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Using DeviceMotion import CoreMotion let motionManager = CMMotionManager()

// Before starting game logic guard motionManager.isDeviceMotionAvailable else {

print("Device Motion is not available.") return }

let myFrame = CMAttitudeReferenceFrame.xArbitraryZVertical guard CMMotionManager.availableAttitudeReferenceFrames().contains(myFrame) else { print("The reference frame XArbitraryZVertical is not available.") return}

Page 144: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Using DeviceMotion import CoreMotion let motionManager = CMMotionManager()

// Before starting game logic guard motionManager.isDeviceMotionAvailable else {

print("Device Motion is not available.") return }

let myFrame = CMAttitudeReferenceFrame.xArbitraryZVertical guard CMMotionManager.availableAttitudeReferenceFrames().contains(myFrame) else { print("The reference frame XArbitraryZVertical is not available.") return}

Page 145: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Gesture Detection Jump

+y

+x

Page 146: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Gesture Detection Jump

+y

+x

Page 147: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Rotate Up

Gesture Detection Jump

+y

+x

Page 148: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Gesture Detection Jump

Page 149: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Rotating rate property

Gesture Detection Jump

Page 150: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Rotating rate property

Detect a pulse

Gesture Detection Jump

Page 151: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Rotating rate property

Detect a pulse

Use the push mechanism

Gesture Detection Jump

Page 152: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Starting DeviceMotion updates using push

motionManager.deviceMotionUpdateInterval = 1 / 50

motionManager.startDeviceMotionUpdates(using: CMAttitudeReferenceFrame.xArbitraryZVertical, to: queue, withHandler: motionHandler)

Page 153: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Starting DeviceMotion updates using push

motionManager.deviceMotionUpdateInterval = 1 / 50

motionManager.startDeviceMotionUpdates(using: CMAttitudeReferenceFrame.xArbitraryZVertical, to: queue, withHandler: motionHandler)

Page 154: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Starting DeviceMotion updates using push

motionManager.deviceMotionUpdateInterval = 1 / 50

motionManager.startDeviceMotionUpdates(using: CMAttitudeReferenceFrame.xArbitraryZVertical, to: queue, withHandler: motionHandler)

Page 155: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Rotation rate

// motionHandler { (deviceMotion: CMDeviceMotion?, error: Error?) in if let error = error { print("Encountered error: \(error!)”)

return }

let rotationRate = deviceMotion.rotationRate var rateAlongHorizontal = rotationRate.y // ... rateAlongHorizontalBuffer.addSample(rateAlongHorizontal) }

Page 156: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Rotation rate

// motionHandler { (deviceMotion: CMDeviceMotion?, error: Error?) in if let error = error { print("Encountered error: \(error!)”)

return }

let rotationRate = deviceMotion.rotationRate var rateAlongHorizontal = rotationRate.y // ... rateAlongHorizontalBuffer.addSample(rateAlongHorizontal) }

Page 157: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Rotation rate

// motionHandler { (deviceMotion: CMDeviceMotion?, error: Error?) in if let error = error { print("Encountered error: \(error!)”)

return }

let rotationRate = deviceMotion.rotationRate var rateAlongHorizontal = rotationRate.y // ... rateAlongHorizontalBuffer.addSample(rateAlongHorizontal) }

Page 158: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Rotation rate

// motionHandler { (deviceMotion: CMDeviceMotion?, error: Error?) in if let error = error { print("Encountered error: \(error!)”)

return }

let rotationRate = deviceMotion.rotationRate var rateAlongHorizontal = rotationRate.y // ... rateAlongHorizontalBuffer.addSample(rateAlongHorizontal) }

Page 159: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Rotation rate

// motionHandler { (deviceMotion: CMDeviceMotion?, error: Error?) in if let error = error { print("Encountered error: \(error!)”)

return }

let rotationRate = deviceMotion.rotationRate var rateAlongHorizontal = rotationRate.y // ... rateAlongHorizontalBuffer.addSample(rateAlongHorizontal) }

Page 160: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Rotation rate

// motionHandler { (deviceMotion: CMDeviceMotion?, error: Error?) in if let error = error { print("Encountered error: \(error!)”)

return }

let rotationRate = deviceMotion.rotationRate var rateAlongHorizontal = rotationRate.y // ... rateAlongHorizontalBuffer.addSample(rateAlongHorizontal) }

Page 161: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Check the buffer

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) { // ...

let didJump = rateAlongHorizontalBuffer.mean() > jumpThreshold // ... }

Page 162: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Check the buffer

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) { // ...

let didJump = rateAlongHorizontalBuffer.mean() > jumpThreshold // ... }

Page 163: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Check the buffer

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) { // ...

let didJump = rateAlongHorizontalBuffer.mean() > jumpThreshold // ... }

Page 164: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Gesture Detection Squat

+y

+x Gravity

Page 165: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Gesture Detection Squat

+y

+x Gravity

Page 166: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Gesture Detection Squat

+y

+x Gravity

Page 167: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Gesture Detection Squat

Page 168: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

User acceleration property

Gesture Detection Squat

Page 169: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

User acceleration property

Regardless of attitude

Gesture Detection Squat

Page 170: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

User acceleration property

Regardless of attitude

Use the push mechanism

Gesture Detection Squat

Page 171: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// User acceleration

// motionHandler { (deviceMotion: CMDeviceMotion?, error: Error?) in

// Rotation rate // ...

let gravity = deviceMotion.gravity let userAcceleration = deviceMotion.userAcceleration

let userAccelerationAlongGravity = userAcceleration.x * gravity.x + userAcceleration.y * gravity.y + userAcceleration.z * gravity.z // ... accelerationAlongGravityBuffer.addSample(userAccelerationAlongGravity) }

Page 172: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// User acceleration

// motionHandler { (deviceMotion: CMDeviceMotion?, error: Error?) in

// Rotation rate // ...

let gravity = deviceMotion.gravity let userAcceleration = deviceMotion.userAcceleration

let userAccelerationAlongGravity = userAcceleration.x * gravity.x + userAcceleration.y * gravity.y + userAcceleration.z * gravity.z // ... accelerationAlongGravityBuffer.addSample(userAccelerationAlongGravity) }

Page 173: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// User acceleration

// motionHandler { (deviceMotion: CMDeviceMotion?, error: Error?) in

// Rotation rate // ...

let gravity = deviceMotion.gravity let userAcceleration = deviceMotion.userAcceleration

let userAccelerationAlongGravity = userAcceleration.x * gravity.x + userAcceleration.y * gravity.y + userAcceleration.z * gravity.z // ... accelerationAlongGravityBuffer.addSample(userAccelerationAlongGravity) }

Page 174: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// User acceleration

// motionHandler { (deviceMotion: CMDeviceMotion?, error: Error?) in

// Rotation rate // ...

let gravity = deviceMotion.gravity let userAcceleration = deviceMotion.userAcceleration

let userAccelerationAlongGravity = userAcceleration.x * gravity.x + userAcceleration.y * gravity.y + userAcceleration.z * gravity.z // ... accelerationAlongGravityBuffer.addSample(userAccelerationAlongGravity) }

Page 175: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// User acceleration

// motionHandler { (deviceMotion: CMDeviceMotion?, error: Error?) in

// Rotation rate // ...

let gravity = deviceMotion.gravity let userAcceleration = deviceMotion.userAcceleration

let userAccelerationAlongGravity = userAcceleration.x * gravity.x + userAcceleration.y * gravity.y + userAcceleration.z * gravity.z // ... accelerationAlongGravityBuffer.addSample(userAccelerationAlongGravity) }

Page 176: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Check the buffer

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) { // ... let didSquat = accelerationAlongGravityBuffer.mean() > squatThreshold // ... }

Page 177: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Check the buffer

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) { // ... let didSquat = accelerationAlongGravityBuffer.mean() > squatThreshold // ... }

Page 178: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Gesture Detection Tilt

+y

+x

+y

+x

Gravity

Page 179: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Gesture Detection Tilt

Tilt+y

+x

+y

+x

Gravity

Page 180: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Gesture Detection Tilt

Page 181: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Current state of the device

Gesture Detection Tilt

Page 182: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Current state of the device

Responsive

Gesture Detection Tilt

Page 183: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Current state of the device

Responsive

Use the pull mechanism

Gesture Detection Tilt

Page 184: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Pull DeviceMotion samples

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) { // ... let deviceMotion = motionManager.deviceMotion let gravity = deviceMotion.gravity

// Component of gravity in the x-z body frame let xzComponent = sqrt(pow(gravity.x, 2) + pow(gravity.z, 2))

let tilt = atan2(gravity.y, xzComponent) }

Page 185: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Pull DeviceMotion samples

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) { // ... let deviceMotion = motionManager.deviceMotion let gravity = deviceMotion.gravity

// Component of gravity in the x-z body frame let xzComponent = sqrt(pow(gravity.x, 2) + pow(gravity.z, 2))

let tilt = atan2(gravity.y, xzComponent) }

Page 186: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Pull DeviceMotion samples

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) { // ... let deviceMotion = motionManager.deviceMotion let gravity = deviceMotion.gravity

// Component of gravity in the x-z body frame let xzComponent = sqrt(pow(gravity.x, 2) + pow(gravity.z, 2))

let tilt = atan2(gravity.y, xzComponent) }

Page 187: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Pull DeviceMotion samples

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) { // ... let deviceMotion = motionManager.deviceMotion let gravity = deviceMotion.gravity

// Component of gravity in the x-z body frame let xzComponent = sqrt(pow(gravity.x, 2) + pow(gravity.z, 2))

let tilt = atan2(gravity.y, xzComponent) }

Page 188: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Pull DeviceMotion samples

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) { // ... let deviceMotion = motionManager.deviceMotion let gravity = deviceMotion.gravity

// Component of gravity in the x-z body frame let xzComponent = sqrt(pow(gravity.x, 2) + pow(gravity.z, 2))

let tilt = atan2(gravity.y, xzComponent) }

Page 189: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

// Stop Device Motion updates

if motionManager.isDeviceMotionActive { motionManager.stopDeviceMotionUpdates() }

Page 190: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch
Page 191: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch
Page 192: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Summary

Page 193: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Summary

Authorization

Page 194: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Summary

Authorization

DeviceMotion

Page 195: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Summary

Authorization

DeviceMotion• Sensor fusion

Page 196: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Summary

Authorization

DeviceMotion• Sensor fusion• Smooth and consistent experience

Page 197: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Summary

Authorization

DeviceMotion• Sensor fusion• Smooth and consistent experience • Performance enhancements

Page 198: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Summary

Authorization

DeviceMotion• Sensor fusion• Smooth and consistent experience • Performance enhancements

Query mechanism

Page 199: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Summary

Authorization

DeviceMotion• Sensor fusion• Smooth and consistent experience • Performance enhancements

Query mechanism•Push: Gesture over time

Page 200: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Summary

Authorization

DeviceMotion• Sensor fusion• Smooth and consistent experience • Performance enhancements

Query mechanism•Push: Gesture over time•Pull: Responsive

Page 201: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Introducing ARKit: Augmented Reality for iOS Hall 3 Tue 5:10PM-6:10PM

What's New in Location Technologies Grand Ballroom B Thu 3:10PM-3:50PM

Related Sessions

Page 202: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Introducing ARKit: Augmented Reality for iOS Hall 3 Tue 5:10PM-6:10PM

What's New in Location Technologies Grand Ballroom B Thu 3:10PM-3:50PM

Advances in SceneKit Rendering WWDC 2016

Related Sessions

Page 203: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

Labs

Core Motion Lab Technology Lab D Thu 4:10PM-6:00PM

Location and Mapping Technologies Lab Technology Lab B Wed 11:00AM-1:00PM

Location and Mapping Technologies Lab Technology Lab K Fri 10:00AM-12:00PM

ARKit Lab Technology Lab A Wed 1:00PM-3:00PM

ARKit Lab Technology Lab A Thu 12:00PM-2:00PM

Page 204: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch

More Informationhttps://developer.apple.com/wwdc17/704

Page 205: 704 Best Practices with Core Motion 05 D - Apple Inc. · Apple Watch Apple Watch Series 1 Apple Watch Series 2 Historical Accelerometer Availability. Available Apple Watch Apple Watch