0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

HappyManaAdvent Calendar 2023

Day 3

SwiftUIでAppleWatchのモーションデータを取得する

Posted at

はじめに

こんにちは、HappyManaです。
AppleWatchでSwiftでモーションデータを取得する方法はあったのですが、SwiftUIに書き直したかったので、SwiftUIでAppleWatchのモーションデータを取得して表示する処理を書きました。

コード

import SwiftUI
import WatchKit
import CoreMotion

struct GetMotionData: View {
    
    private var motion = CMMotionManager()
    
    @State var roll: Double = 0
    @State var pitch: Double = 0
    @State var yaw: Double = 0
    @State var rotationRateX: Double = 0
    @State var rotationRateY: Double = 0
    @State var rotationRateZ: Double = 0
    @State var gravityX: Double = 0
    @State var gravityY: Double = 0
    @State var gravityZ: Double = 0
    @State var accelerationX: Double = 0
    @State var accelerationY: Double = 0
    @State var accelerationZ: Double = 0

    // 取得を開始
    func startUpdates() {
        if motion.isDeviceMotionAvailable {
            motion.deviceMotionUpdateInterval = 1 / 60
            motion.startDeviceMotionUpdates(to: OperationQueue.current!) { [self] (motionData, error) in
                guard let data = motionData else {
                    print("Device Motion Error: \(String(describing: error))")
                    return
                }

                roll = data.attitude.roll
                pitch = data.attitude.pitch
                yaw = data.attitude.yaw
                rotationRateX = data.rotationRate.x
                rotationRateY = data.rotationRate.y
                rotationRateZ = data.rotationRate.z
                gravityX = data.gravity.x
                gravityY = data.gravity.y
                gravityZ = data.gravity.z
                accelerationX = data.userAcceleration.x
                accelerationY = data.userAcceleration.y
                accelerationZ = data.userAcceleration.z
                
                let motionDataString = "\(roll),\(pitch),\(yaw),\(rotationRateX),\(rotationRateY),\(rotationRateZ),\(gravityX),\(gravityY),\(gravityZ),\(accelerationX),\(accelerationY),\(accelerationZ)"
                print(motionDataString)
            }
        }
    }
    // 取得を終了
    func stopUpdates() {
        if motion.isDeviceMotionAvailable {
            motion.stopDeviceMotionUpdates()
        }
    }

    // view
    var body: some View {
        VStack(spacing: 1) {
            Text("roll: \(roll)")
            Text("pitch: \(pitch)")
            Text("yaw: \(yaw)")
            Text("rotationRateX: \(rotationRateX)")
            Text("rotationRateY: \(rotationRateY)")
            Text("rotationRateZ: \(rotationRateZ)")
            Text("gravityX: \(gravityX)")
            Text("gravityY: \(gravityY)")
            Text("gravityZ: \(gravityZ)")
            Text("accelerationX: \(accelerationX)")
            Text("accelerationY: \(accelerationY)")
            Text("accelerationZ: \(accelerationZ)")
        }
        // 画面が表示されたら取得を開始する
        .onAppear {
            startUpdates()
        }
        // 画面が非表示になったら取得を終了する
        .onDisappear {
            stopUpdates()
        }
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?