我正在将指南针构建到我正在开发的 IOS 应用程序中。目前我正在 iPad 上构建,但在 View 中切换方向时遇到了一个大问题
如果我在应用程序已经处于风景/肖像模式时打开 View ,指南针看起来非常好(如第一张图片)但是如果我进入肖像模式然后转向风景,指南针的整个 UI发疯了(见第二张图片)
我在 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);
}
我真的很感激能得到一些帮助,因为它让我发疯!谢谢
最佳答案
我正在为另一个应用程序制作 Qibla 功能,并且有指南针问题,但我不得不说你的问题与指南针功能完全无关。
您说过,当您旋转设备时, View 会变得困惑。这似乎是一个明显的 View 布局问题。根据您是否使用自动布局,我至少建议您首先将所有罗盘 View 添加到一个 View 容器中。该 View 应具有固定大小并在 View 中居中。 (又名 UIAutoResizingMaskFlexibleTopMargin
、BottomMargin
、RightMargin
、LeftMargin
)。
然后,我不确定旋转是否适合您,但我使用 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];
关于ios - 为应用程序创建指南针,但如果方向改变,指南针不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20791815/