美文网首页
指南针的设计(ios)

指南针的设计(ios)

作者: 闯先生的猫 | 来源:发表于2016-03-10 15:21 被阅读207次

import "ViewController.h"

import <CoreLocation/CoreLocation.h>//注:导入定位框架

@interface ViewController ()<CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
UIImageView *compassView;//指南针视图
}
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    //初始化指南针视图
    compassView = [[UIImageView alloc]initWithFrame:self.view.frame];
    compassView.contentMode = UIViewContentModeScaleAspectFit;
    compassView.image = [UIImage imageNamed:@"指南针.jpg"];
    [self.view addSubview:compassView];
     
    if (![CLLocationManager locationServicesEnabled]) {
   return;
    }
    
    locationManager = [[CLLocationManager alloc]init];
    ```
//    判断系统版本
//    iOS8.0之后 需要向用户请求授权

// if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0) {
//
//// 获取设备方向 可以不向用户请求
// [locationManager requestAlwaysAuthorization];

//}
```

// 设置多少米更新一次
locationManager.distanceFilter = 100;

/*
 extern const CLLocationAccuracy kCLLocationAccuracyBest;
 extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
 extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
 extern const CLLocationAccuracy kCLLocationAccuracyKilometer;
 extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;
 */
//定位精度 越精准 耗电量 越大 越发热
//locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
//    系统会帮你 自动管理 开启 关闭定位功能
//locationManager.pausesLocationUpdatesAutomatically = YES;
    locationManager.delegate = self;
//    更新方向
    [locationManager startUpdatingHeading];
    
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
}
//当航向 发生改变的时候 调用
- (void)locationManager:(CLLocationManager *)manager
    didUpdateHeading:(CLHeading *)newHeading{
//    获得地磁方向
    CLLocationDirection direction = newHeading.magneticHeading;
    
//    角度 = 地磁方向*π/180;M_PI
    CGFloat angle = direction*M_PI/180;
    NSLog(@"%f",angle);
    compassView.transform = CGAffineTransformMakeRotation(-angle);
    
}
- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error{
    
}
7251CB54-B515-4BA1-AC32-4D4BCC96B279.png

注:该功能只能在真机上实现,不能再模拟器上运行。

相关文章

  • 指南针的设计(ios)

    import "ViewController.h" import

  • iOS-CoreLocation文集目录

    CoreLocation应用场景:定位iOS8.0之前的定位iOS8.0定位iOS9.0定位定位总结指南针效果区域...

  • Corelocation

    本文章针对iOS8之前、iOS8、以及iOS9.0定位功能的实现,并对指南针、地理反地理编码做个简单介绍 iOS8...

  • iOS指南针

    前言: 这个小项目使用到了CoreLocation框架里面的设备朝向功能,对CoreLocation感兴趣的可以翻...

  • iOS 指南针

    需求:让指南针图片不断指向磁北方向 一、实现思路: 1.获取手机设备朝向(距离磁北方向的角度) 2.让指南针图片反...

  • iOS网络层设计-Engine 实现

    iOS 网络层设计 iOS网络层设计-Client 实现 iOS网络层设计-Engine 实现 iOS 网络层 E...

  • iOS网络层设计-Client 实现

    iOS 网络层设计 iOS网络层设计-Client 实现 iOS网络层设计-Engine 实现 iOS 网络层 C...

  • iOS设计模式(3)适配器模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(4)抽象工...

  • iOS设计模式之美-适配器模式

    iOS设计模式之美-工厂模式iOS设计模式之美-抽象工厂模式iOS设计模式之美-生成器模式iOS设计模式之美-适配...

  • iOS设计模式之美-抽象工厂模式

    iOS设计模式之美-工厂模式iOS设计模式之美-抽象工厂模式iOS设计模式之美-生成器模式iOS设计模式之美-适配...

网友评论

      本文标题:指南针的设计(ios)

      本文链接:https://www.haomeiwen.com/subject/ulutlttx.html