UIView和Coco2d-x场景之间的相互跳转切换(转)
来源:互联网 发布:鞍山北国知春业主群 编辑:程序博客网 时间:2024/06/06 19:24
本文转自:http://cocos2d.9tech.cn/news/2013/1121/38770.html需要注意的是:此篇为cocos2dx 2.X GLVIEW初始化方式。3.X有改动,其它实现是相同的。首先要解释一下这篇文章要讲解的内容:我们在IOS程序中可能要添加一些用cocos2dx实现的功能的话,那么就需要涉及到UIView和Cocos2dx场景之间的切换。那么要如何实现呢?
我们如果在xcode中新建一个cocos2dx项目,在ios文件夹中就可以发现,其实这个cocos2dx就是EAGLView,这是一个UIView。
然后这个view封装到RootViewController中;最后,这个controller在AppController中设置为window的root view controller.
1234567891011121314151617181920212223242526272829303132333435363738- (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
1234567891011#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
1234567891011121314151617181920212223242526272829- (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
1234567891011121314151617- (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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354- (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 场景中:
123//输出当前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的回调方法中:
12345-(void)buttonPressed:(id)sender
{
CCDirector::sharedDirector()->end();
[self.navigationController popViewControllerAnimated:YES];
}
很重要的是第一个语句,先结束掉当前的cocos2dx,然后再调回到原来的view。
好咯,大概的过程就是这个样子了,哥我为了这个过程,浪费了半天的时间,尤其是在cocos2dx场景如何跳转回到UIView的问题上。
原来我是想在cocos2dx的场景中的一个menu item 的回调方法中执行跳回view的功能,但是始终不能实现;
最后我想到不要cocos2dx覆盖整个视图,而是作为一个subview,然后添加一个button,直接让这个对应的controller去处理视图的跳转就可以了。
上面的跳转使用到navigation,如果不想添加这个容器的话,直接在controller之间跳转也是可以的。
下面也给出实现的代码,区别只是在于跳转的代码,其他都一样。
AppController.mm
12345678910111213141516171819202122232425262728- (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
12345-(void)buttonPressed:(id)sender
{
CCDirector::sharedDirector()->end();
[self dismissViewControllerAnimated:YES completion:nil];
}
MyViewController
12345-(void)buttonPressed:(id)sender
{
RootViewController* rootController = [[RootViewController alloc]init];
[self presentViewController:rootController animated:YES completion:nil];
}
下面为了有需要的同志,给出navigation实现的代码下载链接:点击打开链接下载链接:http://download.csdn.net/detail/crayon_dys/6577371(内容只包括ios文件夹)
那么就需要涉及到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,然后再调回到原来的view。
好咯,大概的过程就是这个样子了,哥我为了这个过程,浪费了半天的时间,尤其是在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];
}
- UIView和Coco2d-x场景之间的相互跳转切换
- UIView和Coco2d-x场景之间的相互跳转切换(转)
- Cocos2d-x UIView和Coco2d-x场景之间的相互跳转切换
- cocos2d-x中场景之间的切换
- coco2d-x lua 加入场景切换动画问题
- [转载]Flash场景之间相互跳转的方法
- cocos2d-x 2个场景之间的切换。
- cocos2d-x 自定义场景和场景跳转
- cocos2d-x 自定义场景和场景跳转
- cocos2d-x 场景的切换
- cocos2d-x 场景的切换
- Cocos2d-x 的onEnter() 和 onEnterTransitionDidFinish() 场景延时跳转
- Cocos2d-x 的onEnter() 和 onEnterTransitionDidFinish() 场景延时跳转
- windows中Python2.x和Python3.x之间的切换
- 关于cocos2d-x 和安卓之间的相互调用
- Cocos2d-x 3.0-Director和场景跳转
- sencha touch页面之间的相互跳转
- iOS App 之间的相互跳转
- 用CodeDeploy来做部署、更新和回退
- 社説 20150313 辺野古調査再開 理解得ながら移設を進めたい
- nf_conntrack: table full, dropping packet
- EIGRP笔记
- iOS UITabBarItem Image selected x消失 Xcode 6
- UIView和Coco2d-x场景之间的相互跳转切换(转)
- 安装 SharePoint 2013 with SP1 是的错误解决方法。
- JAVA中包(package)的使用相关
- Android学习笔记•从零开始•第三天•Activity
- Android Volley完全解析(一),初识Volley的基本用法
- 静态路由
- 关于对信号分类后进行小波去噪的尝试(Matlab)
- Matrix Factorization: A Simple Tutorial and Implementation in Python
- 【Git使用详解】Egit插件的安装图解