美文网首页
旋转屏幕设备

旋转屏幕设备

作者: 翘起地球表面 | 来源:发表于2016-02-01 10:41 被阅读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")

    相关文章

      网友评论

          本文标题:旋转屏幕设备

          本文链接:https://www.haomeiwen.com/subject/mceskttx.html