-1
我正在构建一款游戏,要求用户将其设备向下指向,然后指向它们。当用户使用设备的陀螺仪和加速计传感器将用户的设备向下或向前指向时,我将如何检测?如何检测设备是否指向某个方向?
我正在构建一款游戏,要求用户将其设备向下指向,然后指向它们。当用户使用设备的陀螺仪和加速计传感器将用户的设备向下或向前指向时,我将如何检测?如何检测设备是否指向某个方向?
您可以将CoreMotion与Accelerometer结合使用。试试这个:
import Foundation
import CoreMotion
class CoreViewController: UIViewController {
let motionManager = CMMotionManager()
override func viewDidLoad() {
super.viewDidLoad()
motionManager.deviceMotionUpdateInterval = 0.01
motionManager.startDeviceMotionUpdates(to: OperationQueue.current!)
{ deviceManager, error in
print(deviceManager?.userAcceleration.z)
print("Test") // no print
}
print(motionManager.isDeviceMotionActive) // print false
}
}
检查你在Z加速度中得到的值。如果是肯定的,则表示您的手机正面朝下。我没有设备在模拟器上进行测试。检查Z的正值或负值以确认。 编辑:用Swift代码更新。
完成Eric。无法在设备上测试它。 –