zoukankan      html  css  js  c++  java
  • ios5和ios6横竖屏支持及ipad和iphone设备的判断

    ios5和ios6横竖屏支持及ipad和iphone设备的判断

     

    判断是ipad还是iphone设备。此定义在PayViewControllerDemo-Prefix.pch

    定义如下:

     

    #define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)


    #define iPhone UIUserInterfaceIdiomPhone


    #define iPad UIUserInterfaceIdiomPad



    // ios5下的横屏需要调用的函数

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return UIInterfaceOrientationIsLandscape(interfaceOrientation);

    }

    ios5可以在每一个视图中取控制视图的方向,以及当视图切换不同方向时,去控制其子视图的布局,具体可以参照下列方式。

     

     

     

    //界面视图里面有UIImageView和UIButton空间,当横竖屏时,坐标的改变。

    @interfaceViewController ()

    {

        PayViewController *payview;

        UIImageView *drawViewipad;

        UIImageView *drawViewihone;

        UIButton *consumeBtnipone;

        UIButton *consumeBtnipd;

    }


    //6.0之前

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

    {

        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)

        {

            if (isPad == iPad)

            {

                drawViewipad.frame = CGRectMake(306, 150, 412, 120);

                consumeBtnipd.frame = CGRectMake(1024/2-280/2, 550, 280, 50);

            }

            else if (isPad == iPhone)

            {

                drawViewihone.frame = CGRectMake(155, 50, 170, 60);

                consumeBtnipone.frame = CGRectMake(170, 200, 140, 40);

            }

        }

        if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)

        {

            if (isPad == iPad)

            {

                drawViewipad.frame = CGRectMake(200, 150, 412, 120);

                consumeBtnipd.frame = CGRectMake(768/2-280/2, 550, 280, 50);

            }

            else if (isPad == iPhone)

            {

                drawViewihone.frame = CGRectMake(85, 50, 170, 60);

                consumeBtnipone.frame = CGRectMake(90, 200, 140, 40);

            }

        }

        returnYES;

    }


    // ios6下的横屏需要调用的函数

    -(BOOL)shouldAutorotate {

    return YES;

    }

    -(NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskAll;//支持4各方向的旋转。

    }

    然后在plist文件里面找到Supported interface orientations 选项,添加你想支持的方向,都有提示的。

    设置这个选项需要和代码一直,否则会出问题。


    接着看如何控制每一个界面的方向及在不同方向界面的布局,由于ios6不能单独控制每一个界面的方向,所以需要由主viewcontroller来设置界面的控制,如果你有navgation,那么需要定义一个navgation的类,然后在此类中定义控制子viewcontroller的代码,如下:

    //ios6之后需要在top-most controller中来控制方向问题。

     

    -(BOOL)shouldAutorotate

    {

        return [self.viewControllers.lastObjectshouldAutorotate];

    }


    -(NSUInteger)supportedInterfaceOrientations

    {

        return [self.viewControllers.lastObjectsupportedInterfaceOrientations];

    }


    -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

    {

        return [self.viewControllers.lastObjectpreferredInterfaceOrientationForPresentation];

    }

    如果没有navgation,那么就需要在appdelegate中去设置这段代码。

    接着可以在任何子viewcontroller中来控制不同方向的view布局,可以参考子viewcontroller的代码布局,如下:


     

    //ios6之后需要在top-most controller中来控制方向问题。

    - (BOOL)shouldAutorotate

    {

        returnYES;

    }


    - (NSUInteger)supportedInterfaceOrientations

    {

        //判断系统的版本是6.0以上的。

    if ([[UIDevicecurrentDevice]systemVersion].floatValue >= 6.0)

     

        {

    //判断设备当前的方向,然后重新布局不同方向的操作。

            UIInterfaceOrientation currentOrientation = [[UIApplicationsharedApplication] statusBarOrientation];

            if (currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown)

            {

                if (isPad == iPad)

                {

                    drawViewipad.frame = CGRectMake(200, 150, 412, 120);

                    consumeBtnipd.frame = CGRectMake(768/2-280/2, 550, 280, 50);

                }

                else if (isPad == iPhone)

                {

                    drawViewihone.frame = CGRectMake(85, 50, 170, 60);

                    consumeBtnipone.frame = CGRectMake(90, 200, 140, 40);

                }

            }

            if (currentOrientation == UIInterfaceOrientationLandscapeLeft || currentOrientation == UIInterfaceOrientationLandscapeRight)

            {

                if (isPad == iPad)

                {

                    drawViewipad.frame = CGRectMake(306, 150, 412, 120);

                    consumeBtnipd.frame = CGRectMake(1024/2-280/2, 550, 280, 50);

                }

                else if (isPad == iPhone)

                {

                    drawViewihone.frame = CGRectMake(155, 50, 170, 60);

                    consumeBtnipone.frame = CGRectMake(170, 200, 140, 40);

                }

            }

        }

         returnUIInterfaceOrientationMaskAll;

    }


    这样就能控制不同方向的任何操作和界面布局了。


  • 相关阅读:
    曾经看到过一种理论:被爱的一方总是比爱的一方要晚一个季节,所以才会造就许多心酸。
    人生就像在雪地行走,向后看:自己一路走来的轨迹 ;向前看:白茫茫一片; 不要问该往哪里走,只要回答“想往哪里走” ,自己的双脚就是书写历史的工具 ;
    Google的创新九原则(转)
    电影《教父》中的老大哥都觉得:一个不花时间陪家人的男人算不得一个好男人。繁花似锦,岁月青葱,人生不过25亿秒,过一秒,少一秒,时间正滴答滴答地蚕食我们,我们唯一需要做的,就是让这些秒钟过得有意思:高效工作,不断的学习充实,陪陪家人联络亲友,爱惜身体养养花草。
    if you are not making someone else's life better, then you are wasting your time.– Will Smith如果你不能给别人的生活带来改善,那么你就是在浪费你的宝贵时间。 --威尔 史密斯(程序员,你做的东西...)
    要知道其实自己并没有那么出众,你若盛开,清风自来!(低要求,高行动)
    郎永淳妻子抗癌4年路:儿子弃读名校陪治病(转)
    Node.js、Express、Socket.io 入门
    Node.js Express 路由文件分类
    Node.js、express、mongodb 实现分页查询、条件搜索
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3310562.html
Copyright © 2011-2022 走看看