Created
April 23, 2023 07:34
-
-
Save HonmaMasaru/9d7f2e36fb445c21e7c7a118f7a6c3c8 to your computer and use it in GitHub Desktop.
PlaygroundでのCoreMotionの利用 (iPadのPlaygroundのみ)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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