免責事項:この記事はブロガーのオリジナルの記事、再現しているが、記事のソースを明記してください。https://blog.csdn.net/SunFlowerInRain/article/details/78847740
追加することにより、起動しCoreMotion.frameWorkの枠組みを
コード内の基本的な設定情報
#import <CoreMotion/CoreMotion.h>
// 创始化状态,添加Queue来检测状态
- (void)startMotionManager{
if (_cmotionManager == nil) {
_cmotionManager = [[CMMotionManager alloc] init];
}
_cmotionManager.deviceMotionUpdateInterval = 1/15.0;
if (_cmotionManager.deviceMotionAvailable) {
NSLog(@"Device Motion Available");
[_cmotionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
[self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];
}];
}}
//検出器ジャイロスコープ現在の状態に応じて、回転方向決意画面
- (void)handleDeviceMotion:(CMDeviceMotion *)deviceMotion{
double x = deviceMotion.gravity.x;
double y = deviceMotion.gravity.y;
if (fabs(y) >= fabs(x))
{
if (y >= 0){
orientation = UIDeviceOrientationPortraitUpsideDown;
}
else{
orientation= UIDeviceOrientationPortrait;
}
}
else
{
if (x >= 0){
orientation= UIDeviceOrientationLandscapeRight;
}
else{
orientation= UIDeviceOrientationLandscapeLeft;
}
}
}
後に完了
// deallocメソッドをリスニング削除
- (ボイド)のdealloc {
[_cmotionManager stopDeviceMotionUpdates]。
}