我正在尝试制作一个类似指南针的应用程序,但是虽然有明显的差异,但它们并不重要.我需要知道的是:无论手机的方向如何,我怎样才能让iPhone给我罗盘方向(即北方0度)(即如果它平躺在桌子或肖像上,它会给出相同的读数.某人的手,如果指向相同的方式)
TL; DR如何围绕y轴获得iPhone的旋转,每秒左右更新一次.
解决方法
导入CoreLocation
- class ViewController: UIViewController,CLLocationManagerDelegate {
- var lm:CLLocationManager!
- override func viewDidLoad() {
- super.viewDidLoad()
- lm = CLLocationManager()
- lm.delegate = self
- lm.startUpdatingHeading()
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
- func locationManager(manager: CLLocationManager!,didUpdateHeading newHeading: CLHeading!) {
- println(newHeading.magneticHeading)
- }
- }
您可以从https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLHeading_Class/获取更多信息