旋转屏幕设备

2016-02-01  本文已影响57人  翘起地球表面

著作权归作者所有。

作者:臧其龙

链接: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")

上一篇下一篇

猜你喜欢

热点阅读