AR之—悬空指南针

悬浮的指南针.gif
新建CLLocationManager并设置代理
@property(nonatomic , strong) CLLocationManager *locationManager;//定位管理器

- (void)viewDidLoad {
    [super viewDidLoad];
    //管理者的代理
    self.locationManager.delegate = self;
    // 开始获取用户位置 注意:获取用户的方向信息是不需要用户授权的
    [self.locationManager startUpdatingHeading];
}

- (CLLocationManager *)locationManager
{
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
    }
    return _locationManager;
}
当手机方向发生改变时,系统会调用下面方法
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    /*
     magneticHeading 设备与磁北的相对角度
     trueHeading 设置与真北的相对角度, 必须和定位一起使用, iOS需要设置的位置来计算真北
     真北始终指向地理北极点
     */
    // 1.将获取到的角度转为弧度 = (角度 * π) / 180;
    self.pinAngle = newHeading.magneticHeading * M_PI / 180.0;//指针方向
    self.degreeText.string = [NSString stringWithFormat:@"%.0f°",newHeading.magneticHeading];
    SCNMatrix4 transform = SCNMatrix4MakeTranslation(0.3, 0.3, 0.01);
    transform = SCNMatrix4Scale(transform, -0.03, -0.03, 0.03);
    self.degree.transform = transform;
}
ARFrame更新的代理方法
#pragma mark - ARSessionDelegate
- (void)session:(ARSession *)session didUpdateFrame:(ARFrame *)frame {
    SCNMatrix4 transform = SCNMatrix4MakeRotation(self.pinAngle+M_PI, 0,0,1);
    transform = SCNMatrix4Rotate(transform,M_PI_2, -1, 0, 0);
    transform = SCNMatrix4Rotate(transform, M_PI_4, 0, 1, 0);
    transform = SCNMatrix4Scale(transform, 0.5, 0.5, 0.5);
    transform = SCNMatrix4Rotate(transform, -frame.camera.eulerAngles[0], 1, 0, -1);
    transform = SCNMatrix4Translate(transform, 1.0, 0, 1.0);
    self.dashboardNode.transform = transform;//罗盘的位置
    
    SCNMatrix4 pinTransform = SCNMatrix4MakeRotation(M_PI, 0,0,1);
    pinTransform = SCNMatrix4Scale(pinTransform,0.1, 0.125, 0.1);
    pinTransform = SCNMatrix4Rotate(pinTransform,M_PI_2, -1, 0, 0);
    pinTransform = SCNMatrix4Rotate(pinTransform, M_PI_4, 0, 1, 0);
    pinTransform = SCNMatrix4Rotate(pinTransform, -frame.camera.eulerAngles[0], 1, 0, -1);
    pinTransform = SCNMatrix4Translate(pinTransform, 1.0, 0.01, 1.0);

    self.pinNode.transform = pinTransform;//指针的位置
}

demo地址

你可能感兴趣的:(AR之—悬空指南针)