admin管理员组

文章数量:1418148

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
Add a comment  | 

1 Answer 1

Reset to default 1

If 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.

本文标签: swiftHow do I get the iPhone tilt level only to the left and rightStack Overflow