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

CoreMotionを使って気づいたこととiOS11での新機能

 CoreMotionを使って気づいたこととiOS11での新機能

#potatotips

potatotips #42 (iOS/Android開発Tips共有会) - connpass
https://potatotips.connpass.com/event/60015/

Tatsuya Tanaka

July 25, 2017
Tweet

More Decks by Tatsuya Tanaka

Other Decks in Technology

Transcript

  1. CoreMotionΛ
    ࢖ͬͯؾ͍ͮͨ͜ͱͱ
    iOS11Ͱͷ৽ػೳ
    ాத ୡ໵ (@tattn)

    View full-size slide

  2. • Yahoo!৐׵Ҋ಺
    • iOSΞϓϦΤϯδχΞ
    • 2016೥৽ଔ
    • GitHub: @tattn
    • Qiita: @tattn
    • Twitter: @tanakasan2525
    ాத ୡ໵ (@tattn)

    View full-size slide

  3. CoreMotionΛ
    ͕ͬͭΓ৮ͬͯΈ·ͨ͠

    View full-size slide

  4. CoreMotion
    ୺຤ͷ܏͖΍า਺ͳͲɺσόΠεͷಈ͖΍ͦΕ͔Β෼
    ͔Δਓͷಈ͖ͳͲΛऔಘͰ͖ΔϑϨʔϜϫʔΫɻ
    iOS 4͔Βఏڙ͞Ε͍ͯΔɻ

    (iOS 4 Ҏલ͸ UIAccelerometer ͱ͍͏ΫϥεͰՃ଎౓Λऔಘ͍ͯͨ͠)
    Sensing Device Motion in iOS 4
    https://developer.apple.com/videos/play/wwdc2010/423/

    View full-size slide

  5. CoreMotion
    iPhone 5sҎ߱ʹ౥ࡌͷ

    ίϓϩηοα (M7Ҏ߱) Ͱେ෯ػೳڧԽ
    What's New in Core Motion
    https://developer.apple.com/videos/play/wwdc2015/705/
    ←Ճ଎౓ηϯα
    ←δϟΠϩηϯα
    ←࣓ؾηϯα
    ←ߴ౓ηϯα

    View full-size slide

  6. ۩ମతʹͰ͖Δ͜ͱ

    View full-size slide

  7. ଌఆͰ͖Δ͜ͱͷ͢΂ͯ
    Ճ଎౓ηϯα
    δϟΠϩηϯα
    ࣓ؾηϯα
    ߴ౓ηϯα
    • ୺຤ͷ޲͖ (ճస)
    • (୺຤ʹର͢Δ)ॏྗͷํ޲
    • ୺຤ͷՃ଎౓(ॏྗແࢹ)
    • ֤ํ޲ͷ࣓ଋີ౓
    • า਺
    • า͍ͨڑ཭ (ਪఆ)
    • ฏۉ/ݱࡏͷา͘଎͞ [m/s]
    • Ұඵؒͷา਺ [า਺/s]
    • า͍ͯొͬͨ֊਺ (ਪఆ)
    • (ଌఆ͔ͯ͠Βͷ)૬ରߴ౓
    • ؾѹ
    • ׆ಈঢ়ଶ 

    (ఀࢭ/ెา/૸Δ/ࣗಈं/

    ࣗసं)

    View full-size slide

  8. جຊతͳ࢖͍ํ
    1. ηϯαʔͷ஋औಘ։࢝
    2. ஋Λఆظతʹऔಘ(ΫϩʔδϟorϓϩύςΟ)
    ύλʔϯ1
    ύλʔϯ2
    1. ظؒΛࢦఆ
    2. ظؒதʹଌఆͨ͠σʔλΛऔಘ

    View full-size slide

  9. ύλʔϯ1 - ఆظऔಘ
    CMMotionManager().startDeviceMotionUpdates()
    CMMotionManager().startAccelerometerUpdates()
    CMMotionManager().startGyroUpdates()
    CMMotionManager().startMagnetometerUpdates()
    CMPedometer().startUpdates()
    CMPedometer().startEventUpdates()
    CMAltimeter().startRelativeAltitudeUpdates()
    CMMotionActivityManager().startActivityUpdates()
    ෼͔Γ΍͍͢☺औಘ։࢝ϝιου

    View full-size slide

  10. ύλʔϯ1 - ఆظऔಘ
    manager.deviceMotion?.rotationRate
    motionActivityManager.startActivityUpdates(to: .init()) { activity in
    guard let activity = activity else { return }
    if activity.stationary {
    print("ࢭ·ͬͯΔΑ")
    }
    if activity.cycling {
    print("νϟϦͰདྷͨ")
    }
    }
    ෼͔Γ΍͍͢☺औಘํ๏

    View full-size slide

  11. ύλʔϯ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)
    }
    ෼͔Γ΍͍͢☺औಘํ๏

    View full-size slide

  12. ࢖ͬͯΈͯؾ͍ͮͨ͜ͱ

    View full-size slide

  13. ѹ౗త৘ใྔͷগͳ͞
    ϓϩύςΟ໊Ͱݕࡧͯ͠΋
    177݅
    (2017/7/25࣌఺)

    View full-size slide

  14. ༷ʑͳଌఆํ๏͕͋Δ

    View full-size slide

  15. ༷ʑͳଌఆํ๏͕͋Δ
    xArbitraryZVertical
    Z͕࣠ਨ௚ɺX͕࣠ฏ໘ͷ೚ҙํ޲
    xArbitraryCorrectedZVertical
    ࣓ؾηϯαͰyawͷਫ਼౓͕޲্ͨ͠xArbitraryZVertical

    CPU࢖༻཰্͕͕Δ
    xMagneticNorthZVertical
    Z͕࣠ਨ௚ɺX͕࣓࣠๺

    ୺຤Λಈ͔࣓ͯ͠ؾηϯαͷΩϟϦϒϨʔγϣϯ͕ඞཁ
    xTrueNorthZVertical
    Z͕࣠ਨ௚ɺX͕࣠ਅ๺

    ࣓๺ͱਅ๺ͷܭࢉʹ࣓ؾηϯαͱGPSΛ࢖༻

    View full-size slide

  16. ༷ʑͳଌఆํ๏͕͋Δ
    xArbitraryCorrectedZVertical
    ࣓ؾηϯαͰyawͷਫ਼౓͕޲্ͨ͠xArbitraryZVertical

    CPU࢖༻཰্͕͕Δ
    xTrueNorthZVertical
    Z͕࣠ਨ௚ɺX͕࣠ਅ๺

    ࣓๺ͱਅ๺ͷܭࢉʹ࣓ؾηϯαͱGPSΛ࢖༻
    ࣮͸࣓ؾηϯαΛ࢖༻

    (໊લ͔Β͸Θ͔Γʹ͍͘)
    ࣮͸GPSΛ࢖༻

    (GPSͷڐՄΞϥʔτग़ͳ͍ & 

    Ґஔ৘ใαʔϏεͷON͕ඞਢ)

    View full-size slide

  17. υΩϡϝϯτ͕ͨ·ʹࡶ
    վߦ
    https://developer.apple.com/documentation/coremotion/getting_processed_device_motion_data

    View full-size slide

  18. I/F͕ͨ·ʹ΍Δؾͳ͍
    @available(iOS 9.0, *)
    open class CMSensorDataList : NSObject, NSFastEnumeration {
    }
    Swift͔Β࢖͍ʹ͍͘
    protocol NSFastEnumeration {
    func countByEnumerating(
    with state: UnsafeMutablePointer,
    objects buffer: AutoreleasingUnsafeMutablePointer!,
    count len: Int) -> Int
    }

    View full-size slide

  19. I/F͕ͨ·ʹ΍Δؾͳ͍
    @available(iOS 9.0, *)
    open class CMSensorDataList : NSObject, NSFastEnumeration {
    }
    extension CMSensorDataList: Sequence {
    public func makeIterator() -> NSFastEnumerationIterator {
    return NSFastEnumerationIterator(self)
    }
    }
    Tips: ΍ΔؾΛ࣮૷
    ͍ͭ΋ͷ
    for-in, map
    ͳͲ

    View full-size slide

  20. iOS 11Ͱͷ৽ػೳ
    https://developer.apple.com/videos/play/wwdc2017/704/

    View full-size slide

  21. ڐՄঢ়ଶΛࣄલʹऔಘՄೳʹ
    ࠓ·Ͱ
    iOS 11͔Β
    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
    }
    }

    View full-size slide

  22. ίϯύεͷ஋ΛऔಘՄೳʹ
    ๺ʹରͯ͠ͲΕ͚ͩճస͍ͯ͠Δ͔Λ
    0.0° ʙ 360.0 ° ͰऔಘՄೳ
    Heading

    View full-size slide

  23. Ճ଎౓ͷه࿥͕Մೳʹ (7/7+ on iOS11)
    CMSensorRecoder
    • ه࿥࣌ؒΛࢦఆͯ͠ه࿥։࢝ (࠷େ36h)
    • όοΫάϥ΢ϯυͰ50HzͰܭଌ (ΞϓϦΛऴྃͯ͠΋)
    • ࠷େ3೔ؒͷه࿥ΛऔಘՄೳ
    • ࠶౓ه࿥Λ։࢝ͨ͠Γ

    ଞͷΞϓϦ͕ه࿥Λ։࢝͢Δͱ࣌ؒΛԆ௕Մೳ

    View full-size slide

  24. Ճ଎౓ͷه࿥͕Մೳʹ (7/7+ on iOS11)
    CMSensorRecoder
    • Apple Watch
    • Apple Watch Series 1
    • Apple Watch Series 2
    • iPhone 7/7 Plus (iOS 11)
    ॳΊͯiPhoneͰ࢖༻Մೳʹ

    View full-size slide

  25. (´ɾωɾʆ)
    iOS11Ͱͷ৽ػೳΛௐ΂ऴΘͬͨ࣌ͷإ

    View full-size slide

  26. ࠷ޙʹ
    https://github.com/tattn/TryCoreMotion
    ΄΅શػೳ࢖ͬͨαϯϓϧΞϓϦ࡞Γ·ͨ͠

    View full-size slide

  27. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View full-size slide