cocos2dx ios 屏幕适配

CGRect r = [window bounds];
    CGFloat height=r.size.height;
    CGFloat width=r.size.width;
    float ourHeight=960.0f;
    float ourWidth=640.0f;
    float ratio=height/width;
    float our_ratio=ourHeight/ourWidth;
    UIImageView* topView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top.png"]] autorelease];
    UIImageView* bottomView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bottom.png"]] autorelease];
    UIImageView* leftView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"left.png"]] autorelease];
    UIImageView* rightView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]] autorelease];
//    typedef enum {
//        //以下是枚举成员 TestA = 0,
//        blank_top=0,
//        blank_left=1,
//        blank_noneed=2
//    }blankType;//枚举名称
    
    int ourBlankType=0;
    if(ratio>our_ratio)
    {
        //如果屏幕长宽比大于我们游戏长宽比,则需要在上下加图
        ourBlankType=1;
    }
    else
    {
        //屏幕长宽比小于我们的长宽比,在左右加图
        ourBlankType=2;
    }
    // Set RootViewController to window
    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
    {
        // warning: addSubView doesn't work on iOS6
        [window addSubview: _viewController.view];
        UIView* rootView = [[[UIView alloc] initWithFrame:[window bounds]] autorelease];
        
        
        switch (ourBlankType)
        {
            case 1:
            {
                float viewheight=(height- ourHeight*width/ourWidth)/2;
                [topView setFrame:CGRectMake(0, 0, width, viewheight)];
                [_viewController.view addSubview:topView];
                [bottomView setFrame:CGRectMake(0, height-viewheight, width, viewheight)];
                [_viewController.view addSubview:bottomView];
            }
                break;
            case 2:
            {
                float viewWeight=(width- height*ourWidth/ourHeight)/2;
                [leftView setFrame:CGRectMake(0, 0, viewWeight, height)];
                [_viewController.view addSubview:leftView];
                [rightView setFrame:CGRectMake(width-viewWeight, 0, viewWeight, height)];
                [_viewController.view addSubview:rightView];
            }
                break;
            default:break;
        }
        
    }
    else
    {
        // use this method on ios6
        [window setRootViewController:_viewController];
        
        UIView* rootView = [[[UIView alloc] initWithFrame:[window bounds]] autorelease];
        

        switch (ourBlankType)
        {
            case 1:
            {
                float viewheight=(height- ourHeight*width/ourWidth)/2;
                [topView setFrame:CGRectMake(0, 0, width, viewheight)];
                [_viewController.view addSubview:topView];
                [bottomView setFrame:CGRectMake(0, height-viewheight, width, viewheight)];
                [_viewController.view addSubview:bottomView];
            }
                break;
            case 2:
            {
                float viewWeight=(width- height*ourWidth/ourHeight)/2;
                [leftView setFrame:CGRectMake(0, 0, viewWeight, height)];
                [_viewController.view addSubview:leftView];
                [rightView setFrame:CGRectMake(width-viewWeight, 0, viewWeight, height)];
                [_viewController.view addSubview:rightView];
            }
                break;
            default:break;
        }
    }



转载于:https://my.oschina.net/gaoxiang303/blog/268001

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值