iOS 指南针的制作 附带源码

iOS  指南针的制作  附带源码



代码下载地址:  http://pan.baidu.com/share/link?shareid=3088506835&uk=3189484501


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

框架


ViewController.h代码如下:
[cpp]  view plain copy
  1. #import   
  2. #import   
  3. @interface ViewController : UIViewController  
  4.   
  5. @property (retain, nonatomic) UIImageView *compassImageView;  
  6. @property (retain, nonatomic) CLLocationManager *locationManager;  
  7. @end  

ViewController.m代码如下:
[cpp]  view plain copy
  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. }  

[cpp]  view plain copy
  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啦 ! 要在真机上测试哦!!!
转自: http://blog.csdn.net/qqmcy/article/details/9199457


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部