如果方向更改,则为应用创建指南针但罗盘不起作用

时间:2013-12-26 21:57:57

标签: ios objective-c xcode ipad storyboard

我正在为我正在开发的IOS应用程序构建指南针。目前我正在iPad上构建,但在视图中切换方向时面临着一个巨大的问题

如果我在应用程序处于横向/纵向模式时打开视图,指南针看起来非常精细(如第一张图像),但是如果我进入纵向模式,然后转向横向,指南针的整个用户界面发疯了(见第二张图片)

我在ViewDidLoad中的代码看起来像这样:

//start updating compass
locationManager=[[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.headingFilter = 1;
locationManager.delegate=self;
[locationManager startUpdatingHeading];

//get coords of current location
CLLocation *location = [locationManager location];
CLLocationCoordinate2D fromLoc = [location coordinate];

//mecca:
CLLocationCoordinate2D toLoc = [location coordinate];
toLoc = CLLocationCoordinate2DMake(21.4167, 39.8167);


//calculate the bearing between current location and Mecca

float fLat = degreesToRadians(fromLoc.latitude);
float fLng = degreesToRadians(fromLoc.longitude);
float tLat = degreesToRadians(toLoc.latitude);
float tLng = degreesToRadians(toLoc.longitude);

float degree = radiandsToDegrees(atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)));

if (degree >= 0) {
    bearing  = degree;
} else {
    bearing = degree+360;
}
NSLog(@"bearing: %f", bearing);

//rotate the needle from true north
float MnewRad =  degreesToRadians(bearing);
needleImage.transform = CGAffineTransformMakeRotation(MnewRad);//rotate the number of degrees from north

}

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

//compass
float oldRad =  -manager.heading.trueHeading * M_PI / 180.0f;
float newRad =  -newHeading.trueHeading * M_PI / 180.0f;

CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
theAnimation.fromValue = [NSNumber numberWithFloat:oldRad];
theAnimation.toValue=[NSNumber numberWithFloat:newRad];
theAnimation.duration = 0.3f;
[compassView.layer addAnimation:theAnimation forKey:@"animateMyRotation"];
compassView.transform = CGAffineTransformMakeRotation(newRad);

}

我真的很感激这方面的帮助,因为它让我发疯!谢谢

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:3)

我正在为另一个应用程序制作Qibla功能,并且有指南针问题,但我不得不说你的问题与罗盘功能完全无关。

你说,当你旋转设备时,视图会搞砸。这似乎是一个明显的视图布局问题。根据您是否使用自动布局,我建议您首先将所有罗盘视图添加到单个视图容器中。该视图应具有固定大小并以视图为中心。 (又名UIAutoResizingMaskFlexibleTopMarginBottomMarginRightMarginLeftMargin)。

然后,我不确定旋转是否适合您,但我使用transform.rotation.z,因为旋转发生在z轴上。

最后,我建议您将动画持续时间等于位置更新周期,即持续时间= 0.1或其他

顺便说一下,我从使用CoreLocation切换到CoreMotion。它有一个更简单的罗盘API,并且资源要求更低。


如果您需要查看代码,这是我使用CoreLocation的旧代码:

- (int)fixOrientation {
    switch ([[UIApplication sharedApplication] statusBarOrientation]) {
        case UIInterfaceOrientationPortrait:
            return 0;
        case UIInterfaceOrientationLandscapeLeft:
            return 90;
        case UIInterfaceOrientationLandscapeRight:
            return 270;
        case UIInterfaceOrientationPortraitUpsideDown:
            return 180;
    }
}

CLHeading* newHeading = [MCLocationTracker sharedTracker].currentHeading;

[self dismissAlert];

float headingFloat = - newHeading.magneticHeading;
orientationCorrection = [self fixOrientation];
double angle = [[MCLocationTracker sharedTracker] qiblaBearing];

[UIView animateWithDuration:0.05f
                      delay:0.f
                    options:(UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState)
                 animations:^{
                     self.compassImage.transform = CGAffineTransformMakeRotation(toRadian((headingFloat + orientationCorrection)));
                     if (fabs(kMCLocTrackerUndefinedAngle - angle) > 0.1f) {
                         self.arrow.transform = CGAffineTransformMakeRotation(toRadian((headingFloat+angle) + orientationCorrection));
                     }
                 } 
                 completion:NULL];

答案 1 :(得分:0)

每秒多次调用

didUpdateHeading,因此您不需要任何动画。我怀疑你没有看到0.3f秒动画滞后的任何费用。将角度旋转直接设置为新值,它仍然看起来很平滑。