#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
{
}