【发布时间】:2014-01-14 12:31:04
【问题描述】:
我正在将指南针构建到我正在开发的 IOS 应用程序中。目前我正在 iPad 上构建,但在视图中切换方向时面临一个巨大的问题
如果我在应用程序已经处于横向/纵向模式时打开视图,则指南针看起来非常好(如第一张图像)但是如果我进入纵向模式然后转向横向,则指南针的整个 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);
}
非常感谢您对此的帮助,因为它让我发疯!谢谢
【问题讨论】:
标签: ios objective-c xcode ipad storyboard