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;
}
}