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;

    }


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


  • 相关阅读:
    iOS开发之单例模式
    XCode 安装 Alcatraz包管理器失败的处理
    iOS "此证书由未知颁发机构签名"此问题的解决方法
    Android WebView 使用
    BaseActivity
    定时周期执行指定的任务 ScheduledExecutorService
    SQLite数据库浅谈
    android 图片缓存
    Android之drawable state各个属性详解
    Android应用中如何启动另一个应用
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3310562.html
Copyright © 2011-2022 走看看