2015-02-12 56 views
0

我想在我的應用程序中包括一個指南針和標籤主要引力旋轉並保持其作爲iOS應用程序指南針的位置。旋轉UIImageView裏面的指南針像iOS羅盤

iOS 8 Compass

我已經做了一些代碼,並單獨運行,但是當他們一起工作去瘋狂。

這是我使用的代碼的一部分:

的UIImageView * imgCompass; UIImageView * north;

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; 
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.headingFilter = 1; 
[locationManager requestWhenInUseAuthorization]; 
[locationManager startUpdatingLocation]; 
[locationManager startUpdatingHeading]; 

motionManager = [[CMMotionManager alloc] init]; 
motionManager.accelerometerUpdateInterval = 0.01; 
motionManager.gyroUpdateInterval = 0.01; 

[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] 
            withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { 
             if (!error) { 
              [self outputAccelertionData:accelerometerData.acceleration]; 
             } 
             else{ 
              NSLog(@"%@", error); 
             } 
            }]; 
} 

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

    // Convert Degree to Radian and move the needle 
    newRad = -newHeading.trueHeading * M_PI/180.0f; 

    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ 
     self.viewCompass.transform = CGAffineTransformMakeRotation(newRad); 

    } completion:nil]; 
} 


- (void)outputAccelertionData:(CMAcceleration)acceleration{ 
    //UIInterfaceOrientation orientationNew; 

    // Get the current device angle 
    float xx = -acceleration.x; 
    float yy = acceleration.y; 
    float angle = atan2(yy, xx); 


    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
     // Add 1.5 to the angle to keep the image constantly horizontal to the viewer. 
     [self.north setTransform:CGAffineTransformMakeRotation(angle - 1.5)]; 
    } completion:nil]; 


} 

任何想法?謝謝。我絕望......

回答

0

我解決了。只需減去指南針的半徑並將結果添加到所需的對象。在我的情況下,北,東,西,南都是UIImageView。

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { 
    self.lblGrados.text = [NSString stringWithFormat:@"%.0f°", newHeading.magneticHeading]; 

    // Convert Degree to Radian and move the needle 
    newRad = -newHeading.trueHeading * M_PI/180.0f; 

    [UIView animateWithDuration:0.6 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ 
     compassView.transform = CGAffineTransformMakeRotation(newRad); 
    } completion:nil]; 

    [UIView animateWithDuration:0.6 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
     [self.north setTransform:CGAffineTransformMakeRotation(-newRad)]; 
     [self.south setTransform:CGAffineTransformMakeRotation(-newRad)]; 
     [self.east setTransform:CGAffineTransformMakeRotation(-newRad)]; 
     [self.west setTransform:CGAffineTransformMakeRotation(-newRad)]; 
    } completion:nil]; 
}