如何仅在内层使用摄像机视图作为背景

时间:2011-11-01 07:45:13

标签: cocos2d-iphone

我想使用相机作为我的游戏背景,当我将代码放在appDelegate.m中时它正常工作 但是,当我开始游戏时,它显示在每个页面上,当我通过它出现一秒然后背景覆盖相同,但我想只在一个屏幕上显示,这是我的游戏屏幕而不是菜单屏幕。当我把代码在该图层文件上它无法正常工作。我正在使用以下代码:

在appDelegate.h中添加此内容

UIView *overlay;

Add this in appDelegate.m

EAGLView *glView = [EAGLView viewWithFrame:[window bounds] 
    pixelFormat:kEAGLColorFormatRGBA8 depthFormat:0];

Below the [window addSubview: viewController.view]; line of code we will add the following lines of code:
// set the background color of the view
[CCDirector sharedDirector].openGLView.backgroundColor = [UIColor clearColor];
[CCDirector sharedDirector].openGLView.opaque = NO;

// set value for glClearColor
glClearColor(0.0, 0.0, 0.0, 0.0);

// prepare the overlay view and add it to the window
overlay = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
overlay.opaque = NO;
overlay.backgroundColor=[UIColor clearColor];
[window addSubview:overlay];

Next add the following code below the code you just added.

#define CAMERA_TRANSFORM  1.24299

UIImagePickerController *uip;

@try {
    uip = [[[UIImagePickerController alloc] init] autorelease];
    uip.sourceType = UIImagePickerControllerSourceTypeCamera;
    uip.showsCameraControls = NO;
    uip.toolbarHidden = YES;
    uip.navigationBarHidden = YES;
    uip.wantsFullScreenLayout = YES;
    uip.cameraViewTransform = CGAffineTransformScale(uip.cameraViewTransform, 
        CAMERA_TRANSFORM, CAMERA_TRANSFORM);
}
@catch (NSException * e) {
    [uip release];
    uip = nil;
}
@finally {
    if(uip) {
        [overlay addSubview:[uip view]];
        [overlay release];
    }
}


[window bringSubviewToFront:viewController.view];

请帮助找出我可以将此代码放在xxxxxxxx.m中的位置,以便它仅适用于xxxxxxxx.m文件。

谢谢, 拉夫

1 个答案:

答案 0 :(得分:1)

覆盖app delegate类的属性。

从任何地方访问叠加层视图:

YourAppDelegate* appDelegate = (YourAppDelegate*)
                                  [[UIApplication sharedApplication] delegate];
UIView* overlay = appDelegate.overlay;
// add the UIImagePickerController code here …

每当您想要禁用相机视图时,请隐藏叠加视图:

overlay.hidden = YES;