现在的位置: 首页 > 综合 > 正文

iOS 指南针的制作 附带源码

2013年12月15日 ⁄ 综合 ⁄ 共 2066字 ⁄ 字号 评论关闭
iOS  指南针的制作  附带源码







指南针的制作非常简单。
直接看代码吧!
需要添加

<CoreLocation/CoreLocation.h>框架


ViewController.h代码如下:

  1. #import <UIKit/UIKit.h>  
  2. #import <CoreLocation/CoreLocation.h>  
  3. @interface ViewController : UIViewController<CLLocationManagerDelegate>  
  4.   
  5. @property (retain, nonatomic) UIImageView *compassImageView;  
  6. @property (retain, nonatomic) CLLocationManager *locationManager;  
  7. @end  

ViewController.m代码如下:

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.   
  5.     UIImageView* backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BackGroundPad.png"]];  
  6.     [self.view addSubview:backgroundImage];  
  7.     //创建指南针图片  
  8.     self.compassImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Compass_HD.png"]];  
  9.       
  10.     self.compassImageView.center = CGPointMake(370, 500);  
  11.     [self.view addSubview:self.compassImageView];  
  12.     //初始化locationManager并设置代理类  
  13.     self.locationManager = [[CLLocationManager alloc]init];  
  14.     self.locationManager.delegate = self;  
  15.     if ([CLLocationManager headingAvailable]) {  
  16.         //设置精度  
  17.         self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;  
  18.         //设置滤波器不工作  
  19.         self.locationManager.headingFilter = kCLHeadingFilterNone;  
  20.         //开始更新  
  21.         [self.locationManager startUpdatingHeading];  
  22.     }  
  23.     else  
  24.     {  
  25.         UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"atention" message:@"compass not Available" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];  
  26.         [alert show];  
  27.     }  
  28. }  

  1. //调用locationManager成员方法  
  2. - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading  
  3.   
  4. {  
  5.     //重置view的位置  
  6.     self.compassImageView.transform = CGAffineTransformIdentity;        
  7.     CGAffineTransform transform = CGAffineTransformMakeRotation(-1 * M_PI*newHeading.magneticHeading/180.0);  
  8.     self.compassImageView.transform = transform;  
  9. }  

这样就OK啦 ! 要在真机上测试哦!!!

抱歉!评论已关闭.