指南針

#import "RootViewController.h"#import@interface RootViewController ()@property (nonatomic,strong)CLLocationManager *mgr;

@property (nonatomic,strong)UIImageView *imageView;

@end

@implementation RootViewController

- (void)viewDidLoad {

[super viewDidLoad];

// 添加指南針圖片

self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_compasspointer"]];

self.imageView.center = CGPointMake(self.view.center.x, self.view.center.y);

[self.view addSubview:_imageView];

self.mgr.delegate = self;

//? ? 判斷是否是 iOS8

//? ? if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {

//? ? ? ? NSLog(@"是 iOS8");

//? ? ? ? // 主動要求用戶對我們的程序授權(quán) 授權(quán)狀態(tài)改變就會通知代理

//? ? ? ? [self.mgr requestAlwaysAuthorization]; // 請求前臺和后臺定位權(quán)限(必須是 iOS8 才能用)

//? ? ? ? //? ? ? ? [self.mgr requestWhenInUseAuthorization]; // 請求前臺定位權(quán)限

//? ? }else {

//? ? ? ? NSLog(@"是 iOS7");

//? ? }

// 開始獲取用戶位置

// 注意:獲取用戶的方向信息是不需要用戶授權(quán)的

[self.mgr startUpdatingHeading];

// Do any additional setup after loading the view.

}

// 當(dāng)獲取到用戶的方向時就會調(diào)用

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading

{

//? ? NSLog(@"%s",__func__);

/*

magneticHeading 設(shè)備與磁北的相對角度

trueHeading 設(shè)置與真北的相對角度, 必須和定位一起使用, iOS需要設(shè)置的位置來計算真北

真北始終指向地理北極點

磁北對應(yīng)隨著時間變化的地球磁場北極

*/

//? ? NSLog(@"%f",newHeading.magneticHeading);

// 將獲取到的角度轉(zhuǎn)為弧度 = (角度 * pi)/ 180

CGFloat angle = newHeading.magneticHeading * M_PI / 180;

// 旋轉(zhuǎn)圖片

/*

順時針 正

逆時針 負

*/

self.imageView.transform = CGAffineTransformIdentity;

self.imageView.transform = CGAffineTransformMakeRotation(-angle);

}

#pragma mark - 懶加載

- (CLLocationManager *)mgr

{

if (!_mgr) {

_mgr = [[CLLocationManager alloc] init];

}

return _mgr;

}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容