在设备上使用Core Motion旋转视图旋转

时间:2017-12-12 12:38:08

标签: ios swift core-motion

我是CoreMotion API的新手。我希望在设备旋转时旋转我的视图,其中视图将根据速度,速度旋转。从设备旋转获得速度和速度。设备将在平坦的水平表面上。所以轮换应该只考虑Z轴。

我上传了旋转视图的屏幕截图。旋转的橙色视图。

给我正确的方向。

enter image description here

1 个答案:

答案 0 :(得分:0)

将CoreLocation Framework添加到您的项目

1-宣布

@property (nonatomic, strong) CLLocationManager *myLocationManager;

1- init,开始更新标题

 self.myLocationManager = [[CLLocationManager alloc] init];
currentheading = [[CLHeading alloc] init];
[self.myLocationManager startUpdatingHeading];
self.myLocationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
self.myLocationManager.headingFilter = 1;
self.myLocationManager.delegate = self;

3-句柄标题委托//北是imageView指向北

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

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;
[_north.layer addAnimation:theAnimation forKey:@"animateMyRotation"];
 _north.transform = CGAffineTransformMakeRotation(newRad);
 }