我有一个应用程序包含一个针指向世界上任何地方的特定位置的针,但当我使用下面的代码时,它可以工作,但针既旋转又移动,所以如何阻止它移动旋转的时候?
CLLocationCoordinate2D currentLocation;
CLLocationDirection currentHeading;
CLLocationDirection cityHeading;
#define toRad(X) (X*M_PI/180.0)
#define toDeg(X) (X*180.0/M_PI)
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f
- (void)viewDidLoad
{
[super viewDidLoad];
currentHeading=0.0;
// isArabic=[starter isArabic];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if([CLLocationManager locationServicesEnabled]){
[locationManager startUpdatingLocation];
}
[locationManager startUpdatingHeading];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
currentLocation = newLocation.coordinate;
[self updateHeadingDisplays];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading{
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.5f;
[compass.layer addAnimation:theAnimation forKey:@"animateMyRotation"];
compass.transform = CGAffineTransformMakeRotation(newRad);
NSLog(@"%f (%f) => %f (%f)", manager.heading.trueHeading, oldRad, newHeading.trueHeading, newRad);
// if (newHeading.headingAccuracy < 0)
// return;
// Use the true heading if it is valid.
CLLocationDirection theHeading = ((newHeading.trueHeading > 0) ?
newHeading.trueHeading : newHeading.magneticHeading);
cityHeading = [self directionFrom:currentLocation ];
currentHeading = theHeading;
[self updateHeadingDisplays];
}
- (void)updateHeadingDisplays {
// Animate Compass
[UIView animateWithDuration:0.6
delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
CGAffineTransform headingRotation = CGAffineTransformRotate(CGAffineTransformMakeTranslation(0, 0), (CGFloat)toRad(cityHeading)-toRad(currentHeading));
needle.transform = headingRotation;
}
completion:^(BOOL finished) {
}];
}
-(CLLocationDirection) directionFrom: (CLLocationCoordinate2D) startPt {
double lat1 = toRad(startPt.latitude);
double lat2 = toRad(21.4266700 );
double lon1 = toRad(startPt.longitude);
double lon2 = toRad(39.8261100);
double dLon = (lon2-lon1);
double y = sin(dLon) * cos(lat2);
double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
double brng = toDeg(atan2(y, x));
brng = (brng+360);
brng = (brng>360)? (brng-360) : brng;
return brng;
}
答案 0 :(得分:0)
在评论的屏幕截图中,您似乎正在使用自动布局(如您所见)。但随着自动布局出现一定的行为。您可以在大小检查器中看到来自界面构建器的警告:
所选视图没有约束。在构建时,将为视图生成显式左,顶,宽度和高度约束。
谁知道默认情况下对视图应用了哪些约束!如果将图像视图的中心限制在特定位置,则指南针图像应围绕其中心旋转。
例如,您可以控制从图像视图拖动到父级,并在“容器中”选择“垂直”和“水平”方向。