我怎么能像现在的iphone一样创建指南针

时间:2013-01-06 00:10:39

标签: iphone

我想创建一个与当前iphone功能完全相同的指南针应用程序!请问有人指点我的方向吗?我是新人!

1 个答案:

答案 0 :(得分:3)

如何在iOS中创建指南针:

步骤1:创建项目(例如CompassExample)并包含框架

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

ViewContoller.h步骤:

步骤2:在.h文件中,创建位置管理器对象

CLLocationManager *locationManager;
<CLLocationManagerDelegate>
@property (nonatomic,retain) CLLocationManager *locationManager;
@synthesize locationManager;
IBOutlet UIImageView *compassImage;

第3步:Dowload this Compass Image

ViewController.m步骤:

步骤4:在.m文件的viewDidLoad函数中,初始化位置管理器。

    locationManager=[[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.headingFilter = 1;
    locationManager.delegate=self;
    [locationManager startUpdatingHeading];

步骤5:在.m文件中,实现委托功能。

首先,您需要将度数(例如360)转换为弧度(例如3.14 = 2PI)。 第二,乘以-1,以便旋转与扭动手机相反的方向。 第三,应用具有核心动画功能的旋转。在此示例中,我将从当前值旋转到新值。持续时间为0.5秒,如下所示。

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
    // Convert Degree to Radian and move the needle
    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;
    [compassImage.layer addAnimation:theAnimation forKey:@"animateMyRotation"];
    compassImage.transform = CGAffineTransformMakeRotation(newRad);
    NSLog(@"%f (%f) => %f (%f)", manager.heading.trueHeading, oldRad, newHeading.trueHeading, newRad);
}

就是这样!当然,您必须在设备上部署应用程序以检查方向。

此代码的信用证归于kiichi,您可以在Github Link

下载该项目