zoukankan      html  css  js  c++  java
  • iOS设备方向和用户界面方向

    UIDeviceOrientation refers to the physical orientation of the device whereas UIInterfaceOrientation refers to the orientation of the user interface.

    UIDeviceOrientation is a property of the UIDevice class, and there are these possible values:

    UIDeviceOrientationUnknown - Can't be determined 
    UIDeviceOrientationPortrait - Home button facing down
    UIDeviceOrientationPortraitUpsideDown  - Home button facing up
    UIDeviceOrientationLandscapeLeft - Home button facing right
    UIDeviceOrientationLandscapeRight - Home button facing left
    UIDeviceOrientationFaceUp - Device is flat, with screen facing up
    UIDeviceOrientationFaceDown - Device is flat, with screen facing down

    As for UIInterfaceOrientation, it is a property of UIApplication and only contains 4 possibilities which correspond to the orientation of the status bar:

    UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
    
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    
    UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
    
    UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft

    获取设备方向/用户界面方向/controller方向

     获取设备方向UIDeviceOrientation

    [[UIDevice currentDevice] orientation]

    获取用户界面方向 UIInterfaceOrientation

    [[UIApplication sharedApplication] statusBarOrientation] 

     

    需要注意UIDeviceOrientation只是设备物理方向状态,至于应用中是方向还是要以UIInterfaceOrientation为准。用UIDeviceOrientation的时候,UIDeviceOrientationFaceUp、UIDeviceOrientationFaceDown,平放朝上和朝下的处理有问题,会出错。

    获取特定controller的方向

    controller.interfaceOrientation
    区别: http://stackoverflow.com/questions/7968451/different-ways-of-getting-current-interface-orientation

    设备和用户界面方向改变的监听

    // 设备方向改变

     
    [[NSNotificationCenter defaultCenter] addObserver:self                     
                                             selector:@selector(handleOrientChange:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
    ​
    - (void)orientationChanged:(NSNotification *)notification{
       [self adjustViewsForOrientation:[[UIApplication sharedApplication]
                                        statusBarOrientation]];
    }
    ​
    - (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
    ​
        switch (orientation)
        {
            case UIInterfaceOrientationPortrait:
            case UIInterfaceOrientationPortraitUpsideDown:
            { 
            //load the portrait view    
            }
    ​
                break;
            case UIInterfaceOrientationLandscapeLeft:
            case UIInterfaceOrientationLandscapeRight:
            {
            //load the landscape view 
            }
                break;
            case UIInterfaceOrientationUnknown:break;
        }
    }
    ​

    // 用户界面方向改变

    [[NSNotificationCenter defaultCenter] addObserver:self                  selector:@selector(didChangeOrientation:)                                                    name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
    ​
    - (void)didChangeOrientation:(NSNotification *)notification
    {
        UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    ​
        if (UIInterfaceOrientationIsLandscape(orientation)) {
            NSLog(@"Landscape");
        }
        else {
            NSLog(@"Portrait")
        }
    }

    用NSNotificationCenter记得在dealloc方法中remove观察者

    [[NSNotificationCenter defaultCenter] removeObserver:self];

    判断是否竖屏

    BOOL isPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation);

    判断横竖屏

    BOOL isLandscape = self.view.frame.size.width > self.view.frame.size.height;

    当controllers不是全屏时,这种方法有问题。

    作者:sue_zheng

    出处:http://www.cnblogs.com/sueZheng/p/4954938.html

     

  • 相关阅读:
    并发编程学习笔记(15)----Executor框架的使用
    并发编程学习笔记(14)----ThreadPoolExecutor(线程池)的使用及原理
    并发编程学习笔记(13)----ConcurrentLinkedQueue(非阻塞队列)和BlockingQueue(阻塞队列)原理
    并发编程学习笔记(12)----Fork/Join框架
    并发编程学习笔记(11)----FutureTask的使用及实现
    并发编程学习笔记(10)----并发工具类CyclicBarrier、Semaphore和Exchanger类的使用和原理
    设计模式:代理模式
    设计模式:装饰模式
    设计模式:几大原则
    设计模式:策略模式(Strategy)
  • 原文地址:https://www.cnblogs.com/sueZheng/p/4960395.html
Copyright © 2011-2022 走看看