Skip to content

Instantly share code, notes, and snippets.

@HonmaMasaru
Created April 23, 2023 07:34
Show Gist options
  • Save HonmaMasaru/9d7f2e36fb445c21e7c7a118f7a6c3c8 to your computer and use it in GitHub Desktop.
Save HonmaMasaru/9d7f2e36fb445c21e7c7a118f7a6c3c8 to your computer and use it in GitHub Desktop.
PlaygroundでのCoreMotionの利用 (iPadのPlaygroundのみ)
import SwiftUI
import CoreMotion
import PlaygroundSupport
struct ContentView: View {
/// モーションマネージャー
private let motionManager: CMMotionManager = .init()
/// 加速度データ
@State var acceleration: CMAcceleration = .init()
/// Body
var body: some View {
VStack {
Text("x: \(acceleration.x)")
Text("y: \(acceleration.y)")
Text("z: \(acceleration.z)")
}
.onAppear {
if motionManager.isAccelerometerAvailable {
motionManager.accelerometerUpdateInterval = 0.1
motionManager.startAccelerometerUpdates(to: .current!) { data, _ in
guard let data else { return }
acceleration = data.acceleration
}
}
}
}
}
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.setLiveView(ContentView())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment