I'm trying to make circle of objects view in SwiftUI. Want I want to do is every time the device is rotated, the objects inside the circle will move in the direction of the device's rotation angle. Pretty much like if you have a bottle of medicine, when you rotate the bottle, the drugs inside the bottle would move in the direction of the gravity. The view I want to make is pretty much like this:
I'm aware that this uses CoreMotion to achieve this. But I'm not sure what to do yet, and I haven't been able to found an example to achieve this. So far, my progress is this:
import SwiftUI
import CoreMotion
struct GravityView: View {
private let motionManager = CMMotionManager()
private let motionQueue = OperationQueue()
var body: some View {
Text("Hello, World!")
.onAppear(perform: startMotionDetector)
}
private func startMotionDetector() {
motionManager.startGyroUpdates(to: motionQueue) { (data: CMGyroData?, error: Error?) in
guard let data else {
print("Error: \(error!)")
return
}
let rotation: CMRotationRate = data.rotationRate
motionManager.gyroUpdateInterval = 1
print("X Axis: \(rotation.x)")
print("Y Axis: \(rotation.y)")
print("Z Axis: \(rotation.z)")
}
}
}
Could anyone please explain how to implement this?
