iOS实现手动和自动屏幕旋转

 更新时间:2022年07月20日 15:25:39   作者:z15083415803  
这篇文章主要为大家详细介绍了iOS实现手动和自动屏幕旋转,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了iOS实现手动和自动屏幕旋转的具体代码,供大家参考,具体内容如下

首先iPhone中屏幕分为状态栏方向和设备方向

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
};

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
};

系统提供两个地方来设置设备的方向,取两个地方的交集是最后的设备所支持的方向

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window;
-(NSUInteger)supportedInterfaceOrientations;

这里需要注意的是返回的时下面的枚举

typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
};

在转动屏幕的时候会触发下面方法

-(BOOL)shouldAutorotate;

在该方法返回真,自动调用上面的两个方法得到方向。

修改状态栏方向的方法

1、使用私有API setOrientation;
2、修改状态栏的方向,并通过设置View的transform来达到伪旋转的结果,但是设备方向并没有改变
3、主动出发系统支持的方法,就相当于让这个vc在重新出来的时候系统判断所支持的方向的机制重新走一遍。

- (void)awakeSupportInterOrtation:(UIViewController *)showVC completion:(void(^)(void))block
{
    UIViewController *vc = [[UIViewController alloc] init];
    void(^completion)() = ^() {
        [showVC dismissViewControllerAnimated:NO completion:^{
            if (block)
            {
                block();
            }
        }];
    };

    // This check is needed if you need to support iOS version older than 7.0
    BOOL canUseTransitionCoordinator = [showVC respondsToSelector:@selector(transitionCoordinator)];

    if (canUseTransitionCoordinator)
    {
        [showVC presentViewController:vc animated:NO completion:nil];
        [showVC.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
            completion();
        }];
    }
    else
    {
        [showVC presentViewController:vc animated:NO completion:completion];
    }
}

-(NSUInteger)supportedInterfaceOrientations
{
        return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

在需要转为竖屏的时候调用一个方法,在后面两个方法中如上实现,第二个方法中返回的是你最终要转向的方向。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • iOS中设置清除缓存功能的实现方法

    iOS中设置清除缓存功能的实现方法

    清除缓存基本上都是在设置界面的某一个Cell,于是我们可以把清除缓存封装在某一个自定义Cell中,现在位大家介绍一种最基础的清除缓存的方法,感兴趣的朋友一起看看吧
    2017-07-07
  • iOS中实现imageView任意角度旋转的方法

    iOS中实现imageView任意角度旋转的方法

    这篇文章主要给大家介绍了关于iOS中实现imageView任意角度旋转的方法,文中通过示例代码介绍的非常详细,对大家学习或者工作具有一定的参考学习价值,需要的朋友下面随着小编来一起学习学习吧。
    2017-12-12
  • iOS开发探索多线程GCD常用函数

    iOS开发探索多线程GCD常用函数

    这篇文章主要为大家介绍了iOS开发探索多线程GCD常用函数的使用示例介绍,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • iOS 基于AFNetworking下自签名证书配置的方法

    iOS 基于AFNetworking下自签名证书配置的方法

    本篇文章主要介绍了iOS 基于AFNetworking下自签名证书配置的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • IOS应用内跳转系统设置相关界面的方法

    IOS应用内跳转系统设置相关界面的方法

    在iOS开发中,有时会有跳转系统设置界面的需求,例如提示用户打开蓝牙或者WIFI,提醒用户打开推送或者位置权限等,接下来通过本文给大家介绍IOS应用内跳转系统设置相关界面的方法,喜欢的朋友参考下
    2016-02-02
  • UIPageViewController实现的左右滑动界面

    UIPageViewController实现的左右滑动界面

    这篇文章主要为大家详细介绍了UIPageViewController实现的左右滑动界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • ios scrollview嵌套tableview同向滑动的示例

    ios scrollview嵌套tableview同向滑动的示例

    本篇文章主要介绍了ios scrollview嵌套tableview同向滑动的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • iOS App项目中引入SQLite数据库的教程

    iOS App项目中引入SQLite数据库的教程

    SQLite是一个极轻的嵌入式数据库,在应用程序中捆绑使用可以更方便地帮助操控关系型数据,这里我们就来看一下iOS App项目中引入SQLite数据库的教程
    2016-06-06
  • 在iOS开发的Quartz2D使用中实现图片剪切和截屏功能

    在iOS开发的Quartz2D使用中实现图片剪切和截屏功能

    这篇文章主要介绍了在iOS开发的Quartz2D使用中实现图片剪切和截屏功能的方法,代码基于传统的Objective-C,需要的朋友可以参考下
    2015-12-12
  • 基于iOS Realm数据库的使用实例详解

    基于iOS Realm数据库的使用实例详解

    下面小编就为大家分享一篇基于iOS Realm数据库的使用实例详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01

最新评论