2017-06-06 80 views

回答

0

您可以将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代码更新。

+1

完成Eric。无法在设备上测试它。 –