UIView和Coco2d-x场景之间的相互切换
首先要解释一下这篇文章要讲解的内容:我们在IOS程序中可能要添加一些用cocos2dx实现的功能的话,
那么就需要涉及到UIView和Cocos2dx场景之间的切换。那么要如何实现呢?
我们如果在xcode中新建一个cocos2dx项目,在ios文件夹中就可以发现,其实这个cocos2dx就是EAGLView,这是一个UIView。
然后这个view封装到RootViewController中;最后,这个controller在AppController中设置为window的root view controller.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Add the view controller's view to the window and display. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; EAGLView *__glView = [EAGLView viewWithFrame: [window bounds] pixelFormat: kEAGLColorFormatRGBA8 depthFormat: GL_DEPTH_COMPONENT16 preserveBackbuffer: NO sharegroup: nil multiSampling: NO numberOfSamples:0 ]; // Use RootViewController manage EAGLView viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; viewController.wantsFullScreenLayout = YES; viewController.view = __glView; // Set RootViewController to window if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0) { // warning: addSubView doesn't work on iOS6 [window addSubview: viewController.view]; } else { // use this method on ios6 [window setRootViewController:viewController]; } [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusBarHidden: YES]; cocos2d::CCApplication::sharedApplication()->run(); return YES; }
好了,有了上面的了解,那么我们就有办法了。
假如现在我们的要求是:程序启动,第一个视图是UIView,然后点击button,调到cocos2dx的一个场景,然后点击button还可以返回到UIView。
运行的效果是:
点击跳到Scene跳转到第二个图片所示的视图;点击跳到View跳回到第一个图片所示的视图。
实现过程:
(1)很明显这是两个view controller控制下的两个view视图:第一个view只包含了一个button(MyViewController);
第二个view包含了一个cocos2dx视图和一个button(RootViewController)。
(2)所以问题就变成了这两个view之间跳转的问题了,下面我采用navigation controller来处理这二者的切换。
(3)先看看AppController.h
#import "MyViewController.h" @interface AppController : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate,UIApplicationDelegate> @property (nonatomic, retain) UIWindow *window; @property (nonatomic, retain) UINavigationController* navController; @property (nonatomic, retain)MyViewController* myController; @end
然后:AppController.mm
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; myController = [[MyViewController alloc] init]; navController = [[UINavigationController alloc] init]; [navController pushViewController:myController animated:NO]; navController.navigationBarHidden = YES; // Set RootViewController to window if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0) { // warning: addSubView doesn't work on iOS6 [window addSubview:navController.view]; } else { // use this method on ios6 [window setRootViewController:navController]; } [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusBarHidden: YES]; // cocos2d::CCApplication::sharedApplication()->run(); 注意暂时不可以run return YES; }
注意最后面:cocos2d::CCApplication::sharedApplication()->run(); 要注释掉,因为开的时候运行的是第一个view,
此时cocos2dx还没有跑起来。
(4)所以看看这两个视图controller:
① 第一个图片所对应的 MyViewController
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(0, 100, 100, 50); [button setTitle:@"跳到 Scene" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } -(void)buttonPressed:(id)sender { RootViewController* rootController = [[RootViewController alloc]init]; [self.navigationController pushViewController:rootController animated:YES]; }
② 第二个图片所对应的 RootViewController
- (void)viewDidLoad { [super viewDidLoad]; //添加一个button UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(0, 400, 100, 50); [button setTitle:@"跳到 View" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; [self addGLView]; //添加cocos2dx视图 } -(void)buttonPressed:(id)sender { CCDirector::sharedDirector()->end(); [self.navigationController popViewControllerAnimated:YES]; } -(void)addGLView { UIWindow *window = [UIApplication sharedApplication].keyWindow; EAGLView *__glView = [EAGLView viewWithFrame: [window bounds] pixelFormat: kEAGLColorFormatRGBA8 depthFormat: GL_DEPTH_COMPONENT16 preserveBackbuffer: NO sharegroup: nil multiSampling: NO numberOfSamples:0 ]; [__glView setFrame:CGRectMake(0, 0, 320, 320)]; [self.view addSubview:__glView]; cocos2d::CCApplication::sharedApplication()->run(); //这个时候就run } // Override to allow orientations other than the default portrait orientation. // This method is deprecated on ios6 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return UIInterfaceOrientationIsPortrait( interfaceOrientation ); } // For ios6, use supportedInterfaceOrientations & shouldAutorotate instead - (NSUInteger) supportedInterfaceOrientations{ #ifdef __IPHONE_6_0 return UIInterfaceOrientationMaskPortrait; #endif } - (BOOL) shouldAutorotate { return YES; }
(5)下面再具体的看看其中关键的部分:
RootViewController
①我们创键了EAGLView的实例,作为subview添加到当前的view。而且设置它的rect为(0,0,320,320)
那么我们可以在HelloWorld 场景中:
//输出当前cocos2dx的size CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCLog("width = %f;height = %f",winSize.width,winSize.height);
② 创建EAGLView实例的代码可以直接从项目初始的AppController.mm中直接超过了,然后,注意要:
cocos2d::CCApplication::sharedApplication()->run(); 让cocos2dx跑起来。
③注意其中的屏幕方向也要设置一下(为竖向)。
④现在看看,如何从这个view调回到原来的view呢?在button的回调方法中:
-(void)buttonPressed:(id)sender { CCDirector::sharedDirector()->end(); [self.navigationController popViewControllerAnimated:YES]; }
好咯,大概的过程就是这个样子了,哥我为了这个过程,浪费了半天的时间,尤其是在cocos2dx场景如何跳转回到UIView的问题上。
原来我是想在cocos2dx的场景中的一个menu item 的回调方法中执行跳回view的功能,但是始终不能实现;
最后我想到不要cocos2dx覆盖整个视图,而是作为一个subview,然后添加一个button,直接让这个对应的controller去处理视图的跳转就可以了。
上面的跳转使用到navigation,如果不想添加这个容器的话,直接在controller之间跳转也是可以的。
下面也给出实现的代码,区别只是在于跳转的代码,其他都一样。
AppController.mm
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Add the view controller's view to the window and display. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; myController = [[MyViewController alloc] initWithNibName:nil bundle:nil]; // Set RootViewController to window if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0) { // warning: addSubView doesn't work on iOS6 [window addSubview: myController.view]; } else { // use this method on ios6 [window setRootViewController:myController]; } [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusBarHidden: YES]; // cocos2d::CCApplication::sharedApplication()->run(); return YES; }
RootViewController.mm
-(void)buttonPressed:(id)sender { CCDirector::sharedDirector()->end(); [self dismissViewControllerAnimated:YES completion:nil]; }
MyViewController
-(void)buttonPressed:(id)sender { RootViewController* rootController = [[RootViewController alloc]init]; [self presentViewController:rootController animated:YES completion:nil]; }