ios – 快速查找iPhone的指南针方向

前端之家收集整理的这篇文章主要介绍了ios – 快速查找iPhone的指南针方向前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试制作一个类似指南针的应用程序,但是虽然有明显的差异,但它们并不重要.我需要知道的是:无论手机的方向如何,我怎样才能让iPhone给我罗盘方向(即北方0度)(即如果它平躺在桌子或肖像上,它会给出相同的读数.某人的手,如果指向相同的方式)

TL; DR如何围绕y轴获得iPhone的旋转,每秒左右更新一次.

解决方法

导入CoreLocation
  1. class ViewController: UIViewController,CLLocationManagerDelegate {
  2.  
  3. var lm:CLLocationManager!
  4.  
  5. override func viewDidLoad() {
  6. super.viewDidLoad()
  7.  
  8. lm = CLLocationManager()
  9. lm.delegate = self
  10.  
  11. lm.startUpdatingHeading()
  12. }
  13.  
  14. override func didReceiveMemoryWarning() {
  15. super.didReceiveMemoryWarning()
  16. }
  17.  
  18. func locationManager(manager: CLLocationManager!,didUpdateHeading newHeading: CLHeading!) {
  19. println(newHeading.magneticHeading)
  20. }
  21. }

您可以从https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLHeading_Class/获取更多信息

猜你在找的iOS相关文章