Upgrade to Pro — share decks privately, control downloads, hide ads and more …

15分で分かるCore Motionの全て

15分で分かるCore Motionの全て

@iOSDC 2017 Reject Conference days1

#iOSDCRC

Tatsuya Tanaka

October 13, 2017
Tweet

More Decks by Tatsuya Tanaka

Other Decks in Technology

Transcript

  1. CoreMotion iPhone 5sҎ߱ʹ౥ࡌͷ
 ίϓϩηοα (M7Ҏ߱) Ͱେ෯ػೳڧԽ What's New in Core

    Motion https://developer.apple.com/videos/play/wwdc2015/705/ ←Ճ଎౓ηϯα ←δϟΠϩηϯα ←࣓ؾηϯα ←ߴ౓ηϯα
  2. औಘͰ͖Δ৘ใͷ͢΂ͯ Ճ଎౓ηϯα δϟΠϩηϯα ࣓ؾηϯα ߴ౓ηϯα • ୺຤ͷ޲͖ (ճస) • (୺຤ʹର͢Δ)ॏྗͷํ޲

    • ୺຤ͷՃ଎౓(ॏྗআ֎) • ֤ํ޲ͷ࣓ଋີ౓ • า਺ • า͍ͨڑ཭ (ਪఆ) • ฏۉ/ݱࡏͷา͘଎͞ [m/s] • Ұඵؒͷา਺ [า਺/s] • า͍ͯొͬͨ֊਺ (ਪఆ) • (ଌఆ͔ͯ͠Βͷ)૬ରߴ౓ • ؾѹ • ཱͪࢭ·͔ͬͨ/า͖࢝Ί͔ͨ • ׆ಈঢ়ଶ 
 (ఀࢭ/ెา/૸Δ/ࣗಈं/
 ࣗసं) GPS
  3. σʔλऔಘΫϥεͷੜ੒ ཉ͍͠σʔλΛऔಘ͢ΔΫϥεΛੜ੒ // Ճ଎౓ɾδϟΠϩɾ࣓ྗ let motionManager = CMMotionManager() // า਺

    let pedometer = CMPedometer() // ߴ౓ let altimeter = CMAltimeter() // աڈͷՃ଎౓σʔλ let sensorRecoder = CMSensorRecorder() // Ϣʔβʔͷঢ়ଶ let motionActivityManager = CMMotionActivityManager() ↑͜ΕͰ؅ཧΫϥε͸શͯͰ͢ (ൃද࣌)
  4. ػೳͷରԠද T  TT    %FWJDF.PUJPO ˓ ˓

    ˓ ˓ "DDFMFSPNFUFS ˚ SFDPEJOH ˚ SFDPEJOH ˚ SFDPEJOH ˓ (ZSP ˓ ˓ ˓ ˓ .BHOFUPNFUFS ˓ ˓ ˓ ˓ 1FEPNFUFS ˚ qPPSDPVOUJOH  QBDF  DBEFODF  FWFOUUSBDLJOH ˚ FWFOUUSBDLJOH ˓ ˓ "MUJNFUFS ☓ ˓ ˓ ˓ .PUJPO"DUJWJUZ ˓ ˓ ˓ ˓ ※()ͷத͸ඇରԠͳػೳ
  5. ύλʔϯ1 - ఆظऔಘ CMMotionManager().startDeviceMotionUpdates() CMMotionManager().startAccelerometerUpdates() CMMotionManager().startGyroUpdates() CMMotionManager().startMagnetometerUpdates() CMPedometer().startUpdates(from: date) {

    (data, error) in } CMPedometer().startEventUpdates { (event, error) in } CMAltimeter() .startRelativeAltitudeUpdates(to: .init()) { data, _ in } CMMotionActivityManager() .startActivityUpdates(to: .init()) { activity in } औಘ։࢝ϝιουΛݺͼग़͢
  6. ύλʔϯ1 - ఆظऔಘ manager.deviceMotion?.rotationRate motionActivityManager.startActivityUpdates(to: .init()) { activity in guard

    let activity = activity else { return } if activity.stationary { print("ࢭ·ͬͯΔΑ") } if activity.cycling { print("νϟϦͰདྷͨ") } } ϓϩύςΟ·ͨ͸ΫϩʔδϟͰ஋Λड͚औΔ
  7. ύλʔϯ2 - ظؒͰऔಘ let now = Date() let dayBeforeYesterDay =

    now.addingTimeInterval(-60 * 60 * 24 * 2) pedometer.queryPedometerData(from: dayBeforeYesterDay, to: now) { data, error in guard let data = data, error == nil else { return } print(data.distance ?? 0) print(data.numberOfSteps) print(data.floorsAscended ?? 0) } (from:to)ͰظؒΛࢦఆ͢Δ
  8. CoreMotionͷڐՄঢ়ଶͷνΣοΫ pedometer.queryPedometerData(from: now, to: now) { (data, error) in if

    let code = error?._code, code == Int(CMErrorMotionActivityNotAuthorized.rawValue) { // ະڐՄ } iOS10·Ͱ͸
 errorͰ൑ఆ͢Δ͔͋͠Γ·ͤΜͰͨ͠ʘ(^o^)ʗ
  9. ڐՄঢ়ଶΛࣄલʹऔಘՄೳʹ ࠓ·Ͱ iOS 11 / watchOS 4͔Β pedometer.queryPedometerData(from: now, to:

    now) { (data, error) in if let code = error?._code, code == Int(CMErrorMotionActivityNotAuthorized.rawValue) { // ະڐՄ } if CMPedometer.isStepCountingAvailable() { switch CMPedometer.authorizationStatus() { case .notDetermined: break case .restricted: break case .denied: break case .authorized: break } }
  10. Operation Queueʹ஫ҙ ͲͷOperation QueueͰσʔλΛड͚औΔ͔ΛࢦఆͰ͖Δ motionManager.startMagnetometerUpdates(to: .main) { (data, error) in

    // ϝΠϯεϨου } ↑OperationQueue.main͸σʔλऔಘͷͨͼʹ
 UIεϨουΛϒϩοΩϯά͢Δ͜ͱʹͳΔɻɻ Because the processed events might arrive at a high rate, using the main operation queue is not recommended. https://developer.apple.com/documentation/coremotion/cmmotionmanager/1615968-startmagnetometerupdates
  11. ػೳͷରԠද ※()ͷத͸ඇରԠͳػೳ T  TT    %FWJDF.PUJPO ˓

    ˓ ˓ ˓ "DDFMFSPNFUFS ˚ SFDPEJOH ˚ SFDPEJOH ˚ SFDPEJOH ˓ (ZSP ˓ ˓ ˓ ˓ .BHOFUPNFUFS ˓ ˓ ˓ ˓ 1FEPNFUFS ˚ qPPSDPVOUJOH  QBDF  DBEFODF  FWFOUUSBDLJOH ˚ FWFOUUSBDLJOH ˓ ˓ "MUJNFUFS ☓ ˓ ˓ ˓ .PUJPO"DUJWJUZ ˓ ˓ ˓ ˓