旋转屏幕设备

著作权归作者所有。

作者:臧其龙

链接:http://www.zhihu.com/question/31376539/answer/51667764

1.如何锁屏或者不支持横屏的时候检测手机方向?

- (void)applicationDidBecomeActive:(UIApplication *)application

{

[_motionManager startDeviceMotionUpdates];

}

- (void)applicationWillResignActive:(UIApplication *)application

{

[_motionManager stopDeviceMotionUpdates];

}

- (UIDeviceOrientation)realDeviceOrientation

{

CMDeviceMotion *deviceMotion = _motionManager.deviceMotion;

double x = deviceMotion.gravity.x;

double y = deviceMotion.gravity.y;

if (fabs(y) >= fabs(x))

{

if (y >= 0)

return UIDeviceOrientationPortraitUpsideDown;

else

return UIDeviceOrientationPortrait;

}

else

{

if (x >= 0)

return UIDeviceOrientationLandscapeRight;

else

return UIDeviceOrientationLandscapeLeft;

}

}

2.如何手动旋转设备?

Objective-C:

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];

[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

Swift:

let value = UIInterfaceOrientation.LandscapeLeft.rawValue

UIDevice.currentDevice().setValue(value, forKey: "orientation")

你可能感兴趣的:(旋转屏幕设备)