Can anyone tell me how to make it so that only when you turn left or right the position of the object changes. Because with this code it partially works, but when the phone is in a horizontal position and moving it up or down in this position, the position of the object changes, which I do not need.
To make it clearer, I add a picture of how I want to change the position of the iPhone and after this position the object on the screen should rotate
import SwiftUI
import CoreMotion
struct LevelView: View {
@StateObject private var motionManager = MotionManager()
var body: some View {
GeometryReader { geometry in
ZStack {
VStack(spacing: 0) {
Rectangle()
.fill(Color.white)
.frame(height: geometry.size.height / 2)
Rectangle()
.fill(Color.orange)
.frame(height: geometry.size.height / 2)
}
.rotationEffect(.degrees(motionManager.tiltAngle), anchor: .center)
}
}
}
}
class MotionManager: ObservableObject {
private var motion = CMMotionManager()
@Published var tiltAngle: Double = 0.0
init() {
startMotionUpdates()
}
private func startMotionUpdates() {
if motion.isDeviceMotionAvailable {
motion.deviceMotionUpdateInterval = 1.0 / 60.0 // 60 FPS
motion.startDeviceMotionUpdates(to: .main) { (data, error) in
if let data = data {
DispatchQueue.main.async {
let gravity = data.gravity
print(gravity)
let rollAngle = gravity.x * 180 / .pi
self.tiltAngle = rollAngle
}
}
}
}
}
}
Can anyone tell me how to make it so that only when you turn left or right the position of the object changes. Because with this code it partially works, but when the phone is in a horizontal position and moving it up or down in this position, the position of the object changes, which I do not need.
To make it clearer, I add a picture of how I want to change the position of the iPhone and after this position the object on the screen should rotate
import SwiftUI
import CoreMotion
struct LevelView: View {
@StateObject private var motionManager = MotionManager()
var body: some View {
GeometryReader { geometry in
ZStack {
VStack(spacing: 0) {
Rectangle()
.fill(Color.white)
.frame(height: geometry.size.height / 2)
Rectangle()
.fill(Color.orange)
.frame(height: geometry.size.height / 2)
}
.rotationEffect(.degrees(motionManager.tiltAngle), anchor: .center)
}
}
}
}
class MotionManager: ObservableObject {
private var motion = CMMotionManager()
@Published var tiltAngle: Double = 0.0
init() {
startMotionUpdates()
}
private func startMotionUpdates() {
if motion.isDeviceMotionAvailable {
motion.deviceMotionUpdateInterval = 1.0 / 60.0 // 60 FPS
motion.startDeviceMotionUpdates(to: .main) { (data, error) in
if let data = data {
DispatchQueue.main.async {
let gravity = data.gravity
print(gravity)
let rollAngle = gravity.x * 180 / .pi
self.tiltAngle = rollAngle
}
}
}
}
}
}
Share
Improve this question
asked Jan 31 at 8:38
OlehOleh
231 silver badge4 bronze badges
1 Answer
Reset to default 1If I understand your problem correctly, I assume the behaviour you are experiencing might be happening because data.gravity
is a property denoting the acceleration vector related to the gravitational force acting on the device.
Documentation:
The gravity acceleration vector expressed in the device’s reference frame. (...) The total acceleration of the device is equal to gravity plus the acceleration the user imparts to the device (userAcceleration).
What you need is a vector denoting static position in a 3D space and not an acceleration.
You could leverage another property, which will be more aligned with your goal - data.altitude
of type CMAltitude
. The CMAltitude.roll
property will give you the actual left and right rotation of the device.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745274528a4619954.html
评论列表(0条)