ios 방향 감지 코드

1250 단어 ios
1, 프레임 추가 CoreLocation.framework
2, ViewController.h에서 다음과 같은 코드를 쓴다
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
    CLLocationManager * _locationManger;
}
@property (retain, nonatomic) IBOutlet UILabel *header;

@end

 3,ViewController.m 코드는 다음과 같습니다.
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    _locationManger = [[CLLocationManager alloc]init];
    
    if([CLLocationManager headingAvailable]){
        _locationManger.headingFilter = 0.1 ;//    ,     0.1    
        [_locationManger startUpdatingHeading];
        
    }
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    if(newHeading.headingAccuracy>0){
        CLLocationDirection theHeding = newHeading.magneticHeading;
        _header.text = [NSString stringWithFormat:@"%lf ",theHeding];
    
    }  
}

좋은 웹페이지 즐겨찾기