@interface FKViewController () <CLLocationManagerDelegate>
{
CALayer* znzLayer;
}
@property (nonatomic , strong)CLLocationManager *locationManager;
@end
@implementation FKViewController
- (
void
)viewDidLoad
{
[super viewDidLoad];
if
([CLLocationManager headingAvailable])
{
znzLayer = [[CALayer alloc] init];
NSInteger screenHeight = [UIScreen mainScreen].bounds.size.height;
NSInteger y = (screenHeight - 320) / 2;
znzLayer.frame = CGRectMake(0 , y , 320, 320);
znzLayer.contents = (id)[[UIImage imageNamed:@
"znz.png"
] CGImage];
[self.view.layer addSublayer:znzLayer];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingHeading];
}
else
{
[[[UIAlertView alloc] initWithTitle:@
"提醒"
message:@
"您的设备不支持磁力计"
delegate:self
cancelButtonTitle:@
"确定"
otherButtonTitles: nil]
show];
}
}
-(
void
)locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading
{
CGFloat headings = -1.0f * M_PI * newHeading.magneticHeading / 180.0f;
CABasicAnimation* anim = [CABasicAnimation
animationWithKeyPath:@
"transform"
];
CATransform3D fromValue = znzLayer.transform;
anim.fromValue = [NSValue valueWithCATransform3D: fromValue];
CATransform3D toValue = CATransform3DMakeRotation(headings , 0 , 0 , 1);
anim.toValue = [NSValue valueWithCATransform3D: toValue];
anim.duration = 0.5;
anim.removedOnCompletion = YES;
znzLayer.transform = toValue;
[znzLayer addAnimation:anim forKey:nil];
}
-(
BOOL
)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager
{
return
YES;
}
@end