zoukankan      html  css  js  c++  java
  • Code Snippets for iOS Device Orientation

    iOS Code Snippets 看来的技术,挺方便的,转载记录于此。

    在XCode4中,项目属性设置中很容易就可以配置iOS项目支持设备持有方向,如图: Snip20120827 2

    可惜,这个设置仅仅是在plist中存储了相关设置,真正要控制某个UIView的设备翻转支持,你还得在相关的UIViewController中折腾-shouldAutorotateToInterfaceOrientation:函数,根据不同的设备持有方向,来返回YES或NO。

    这个code snippet简化了相关操作,通过它你可以直接在shouldAutorotateToInterfaceOrientation:函数中查询plist的相关设置,根据设置来进行返回,而不用手工代码来进行一一判断。

    static inline NSString *    NSStringFromUIInterfaceOriention(UIInterfaceOrientation orientation)
    {
        switch (orientation) {
            case UIInterfaceOrientationPortrait:
                return @"UIInterfaceOrientationPortrait";
    
            case UIInterfaceOrientationPortraitUpsideDown:
                return  @"UIInterfaceOrientationPortraitUpsideDown";
    
            case UIInterfaceOrientationLandscapeLeft:
                return @"UIInterfaceOrientationLandscapeLeft";
    
            case UIInterfaceOrientationLandscapeRight:
                return @"UIInterfaceOrientationLandscapeRight";
    
            default:
                return @"Unexpected";
        }
    }
    
    
    static inline BOOL UIInterfaceOrientationIsSupportedOrientation(UIInterfaceOrientation interfaceOrientation)
    {
        NSArray *array = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
        NSUInteger index = [array indexOfObject:NSStringFromUIInterfaceOriention(interfaceOrientation)];
        return index != NSNotFound;
    }

    在shouldAutorotateToInterfaceOrientation:中进行使用:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return UIInterfaceOrientationIsSupportedOrientation(interfaceOrientation);
    }

    又及:有人讨厌全局函数,那么也可以考虑将其封闭到UIViewController中去,作为一个Category来存在。调用的时候加个self就成了,也蛮美观的。

  • 相关阅读:
    回顾[2007-09-03 12:58:03]
    关于知音[2007-08-17 20:56:06]
    今天晚上吃散伙饭[2007-06-18 00:24:36]
    上次所料不错[2007-06-13 15:44:47]
    今天真没劲[2007-06-10 17:50:25]
    关于昨晚的梦[2007-05-07 12:12:06]
    iOS 自定义键盘
    iOSQuart2D绘图之UIImage简单使用
    iOS 两种不同的图片无限轮播
    iOS 简单引导界面
  • 原文地址:https://www.cnblogs.com/wupher/p/2659282.html
Copyright © 2011-2022 走看看