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
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.
本文标签: swiftHow do I get the iPhone tilt level only to the left and rightStack Overflow
版权声明:本文标题:swift - How do I get the iPhone tilt level only to the left and right? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745274527a2651099.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论