ios指南针

参考http://blog.sina.com.cn/s/blog_4a37054201013nhr.html
可运行的代码如下
1.拖拽ImageView ,关联变量到.h中
2.代码如下

//
//  ViewController.h
//  SouthDemo
//
//  Created by xiao7 on 14/10/25.
//  Copyright (c) 2014年 killinux. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController<CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) IBOutlet UIImageView *arrowImageView;

@end


[/code


//
//  ViewController.m
//  SouthDemo
//
//  Created by xiao7 on 14/10/25.
//  Copyright (c) 2014年 killinux. All rights reserved.
//

#import "ViewController.h"


@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    //NSLog(@"SourceDemo");
    _arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"compass.png"]];
    
    _arrowImageView.center = CGPointMake(160, 240);
    
    [self.view addSubview:_arrowImageView];
    self.locationManager= [[CLLocationManager alloc]init];
    self.locationManager.delegate = self;
    
    if ([CLLocationManager headingAvailable]) {
        //设置精度
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        //设置滤波器不工作
        self.locationManager.headingFilter = kCLHeadingFilterNone;
        //开始更新
        [self.locationManager startUpdatingHeading];
    }

    
}
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    NSLog(@"didUpdateHeading---->");
    //每次要重置view的位置,才能保证图片每次偏转量正常,而不是叠加,指针方向正确。
    _arrowImageView.transform = CGAffineTransformIdentity;
    
    CGAffineTransform transform = CGAffineTransformMakeRotation(-1 * M_PI*newHeading.magneticHeading/180.0);
    
    
    //    CGAffineTransform transform = CGAffineTransformMakeTranslation(10, 50);
    _arrowImageView.transform = transform;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

猜你喜欢

转载自haoningabc.iteye.com/blog/2147902
今日推荐