![](/img/trans.png)
[英]ios UIImageView rotation direction in swift at variable degree in clockwise direction
[英]iOS ARKit - Calculate the degree of rotation of device in all three direction
我想得到所有三軸的旋轉度。 我試圖用橫向模式實現。 當從左向右旋轉設備(Y旋轉)時,我想要在用戶設置左端和旋轉到右邊時覆蓋度數。 所以當達到起點時我們需要360度。 因此,對於y旋轉,使用以下計算,它很好。
let q = sceneView.pointOfView.orientation
let yRotation = Float.pi - atan2f( (2*q.y*q.w)-(2*q.x*q.z), 1-(2*pow(q.y,2))-(2*pow(q.z,2)) )
同樣,想要Z旋轉,對於Z旋轉,不需要360度旋轉。 但旋轉約90度時需要准確的值如果我們使用的話,這是好的
var zRotation = sceneView.pointOfView.eulerAngles.z
但是這將是錯誤的*如果我們在某種程度上從左到右(Y方向)旋轉設備然后檢查Z旋轉,我們將使Z旋轉值接近180度而不是接近0
所以嘗試了一種不好的方法來獲得Z旋轉:
var zRotation = sceneView.pointOfView.eulerAngles.z
if abs(sceneView.pointOfVieweulerAngles.z.toDegree) > 90 {
zRotation = (abs(cameraNode.eulerAngles.z.toDegree) - 180).toRadian
}
但這不准確,我們可以在減去180時看到Z旋轉值。
你能建議一個合適的方法嗎?
X旋轉值與Z旋轉相似,在從左向右(Y旋轉)旋轉一定程度后檢查時值接近180。
謝謝
編輯:2109年9月3日對於x和y,現在我正在使用
let xRotation = sceneView!.session.currentFrame!.camera.eulerAngles.x
let zRotation = sceneView!.session.currentFrame!.camera.eulerAngles.z
如果禁用橫向(左和右),您將獲得非常好的x,y和z旋轉數據。
if let q = self?.sceneView.pointOfView?.orientation {
let heading = atan2f( (2 * q.y * q.w) - (2 * q.x * q.z), 1 - (2 * pow(q.y,2)) - (2 * pow(q.z,2)) )
let attitude = asinf( (2 * q.x * q.y) + (2 * q.z * q.w) )
let bank = atan2f( (2 * q.x * q.w) - (2 * q.y * q.z), 1 - (2 * pow(q.x,2)) - (2 * pow(q.z,2)) )
print("X: \(attitude * (180.0 / .pi)) Y: \(heading * (180.0 / .pi)) Z: \(bank * (180.0 / .pi))")
}
如果您允許橫向方向,那么您將獲得不同的值。 一旦設備從縱向移動到橫向,您將獲得旋轉數據,就像整個坐標空間已經旋轉pi / 2弧度一樣。 然后你必須調整數據(Add或Substract pi / 2)以獲得正確的值。 有沒有辦法在更改為橫向方向后轉換坐標系?
因此,如果您不希望設備窗口按方向旋轉,請剖析橫向模式。 雖然您可以獲取所有方向信息來執行任何UI更改:
if UIDevice.current.orientation == UIDeviceOrientation.faceUp {
print("faceUp")
} else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {
print("landscapeRight")
} else if UIDevice.current.orientation == UIDeviceOrientation.portrait {
print("portrait")
} else if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown {
print("portraitUpsideDown")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.