zoukankan      html  css  js  c++  java
  • 单个APP页面支持屏幕旋转

    1、info中支持所有的方向

    2、APPDelega.h中添加属性

    @property (nonatomic,assign) BOOL allowRotate;

    APPdelegate.m中实现方法

    //此方法会在设备横竖屏变化的时候调用
    - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
        if (_allowRotate) {
            return UIInterfaceOrientationMaskAll;
        }else{
            return UIInterfaceOrientationMaskPortrait;
        }
    }

    3、在需要旋转的单个页面中添加

    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        delegate.allowRotate = YES;
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        delegate.allowRotate = NO;
        
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]){
            SEL selector = NSSelectorFromString(@"setOrientation:");
            NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
            [invocation setSelector:selector];
            [invocation setTarget:[UIDevice currentDevice]];
            int val = UIInterfaceOrientationPortrait;
            [invocation setArgument:&val atIndex:2];
            [invocation invoke];
        }
    }
    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    //        屏幕从竖屏变为横屏时执行
            NSLog(@"屏幕从竖屏变为横屏");
            
        }else{
    //        屏幕从横屏变为竖屏时执行
            NSLog(@"屏幕从横屏变为竖屏");
        }
    }
    
    
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
        // do something after rotation
        
    }

    后话

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
    - (void)orientChange:(NSNotification *)notification {
        UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
        if (orientation == UIDeviceOrientationLandscapeLeft) {
            NSLog(@"notification: 屏幕从竖屏变为横屏");
        } else if (orientation == UIDeviceOrientationLandscapeRight) {
            NSLog(@"notification: 屏幕从竖屏变为横屏");
        } else if (orientation == UIDeviceOrientationPortrait) {
            NSLog(@"notification: 屏幕从横屏变为竖屏");
        }
    }
  • 相关阅读:
    Git中清除远程仓库HTTPS认证信息的方法
    JDK8新增时间类型用在JPA中的问题
    5 个关于 API 中日期和时间设计规则
    时间标准基础知识UTC和ISO8601
    JDK8中的时间API
    2019第7周日
    顶级思维模式:推导事物的第一性原理
    JS的jsoneditor,用来操作Json格式的界面;json-editor用来根据json数据生成界面
    Java读写文件,中文乱码解决
    intellij idea 热部署、热加载设置方法
  • 原文地址:https://www.cnblogs.com/fengmin/p/6429533.html
Copyright © 2011-2022 走看看