---
title: "ios指南针"
author: "haoningabc"
date: "2014-10-25T17:06:22Z"
tags: ["iOS"]
categories: ["移动开发"]
slug: "ios"
draft: false
lastmod: "2014-10-25T17:06:21Z"
id: 2147902
url: "https://haoningabc.iteye.com/blog/2147902"
---

参考http://blog.sina.com.cn/s/blog\_4a37054201013nhr.html 可运行的代码如下 1.拖拽ImageView ,关联变量到.h中 2.代码如下 \[code="java"] // // ViewController.h // SouthDemo // // Created by xiao7 on 14/10/25. // Copyright (c) 2014年 killinux. All rights reserved. // #import #import @interface ViewController : UIViewController @property (strong, nonatomic) CLLocationManager \*locationManager; @property (strong, nonatomic) IBOutlet UIImageView \*arrowImageView; @end \[/code \[code="java"] // // ViewController.m // SouthDemo // // Created by xiao7 on 14/10/25. // Copyright (c) 2014年 killinux. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { \[super viewDidLoad]; //NSLog(@"SourceDemo"); \_arrowImageView = \[\[UIImageView alloc] initWithImage:\[UIImage imageNamed:@"compass.png"]]; \_arrowImageView.center = CGPointMake(160, 240); \[self.view addSubview:\_arrowImageView]; self.locationManager= \[\[CLLocationManager alloc]init]; self.locationManager.delegate = self; if (\[CLLocationManager headingAvailable]) { //设置精度 self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; //设置滤波器不工作 self.locationManager.headingFilter = kCLHeadingFilterNone; //开始更新 \[self.locationManager startUpdatingHeading]; } } -(void)locationManager:(CLLocationManager \*)manager didUpdateHeading:(CLHeading \*)newHeading { NSLog(@"didUpdateHeading----&gt;"); //每次要重置view的位置，才能保证图片每次偏转量正常，而不是叠加，指针方向正确。 \_arrowImageView.transform = CGAffineTransformIdentity; CGAffineTransform transform = CGAffineTransformMakeRotation(-1 * M\_PI\*newHeading.magneticHeading/180.0); // CGAffineTransform transform = CGAffineTransformMakeTranslation(10, 50); \_arrowImageView.transform = transform; } - (void)didReceiveMemoryWarning { \[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end \[/code]