位置: 编程技术 - 正文

UIView和Coco2d-x场景之间的相互跳转切换(转)(uiview和calayer关系)

编辑:rootadmin
本文转自: 2.X GLVIEW初始化方式。3.X有改动,其它实现是相同的。首先要解释一下这篇文章要讲解的内容:我们在IOS程序中可能要添加一些用cocos2dx实现的功能的话,

推荐整理分享UIView和Coco2d-x场景之间的相互跳转切换(转)(uiview和calayer关系),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:cocos2d与unity3d,cocos2d和unity2d,cocos2d和unity2d,cocos2d与unity3d,uiview和calayer关系,uiviewpropertyanimator,uiviewcontentmode,uiview和uiviewcontroller,内容如对您有帮助,希望把文章链接给更多的朋友!

那么就需要涉及到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_COMPONENT 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(); returnYES;}

好了,有了上面的了解,那么我们就有办法了。

假如现在我们的要求是:程序启动,第一个视图是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 returnYES;}

注意最后面:cocos2d::CCApplication::sharedApplication()->run(); 要注释掉,因为开的时候运行的是第一个view,

此时cocos2dx还没有跑起来。

(4)所以看看这两个视图controller:

UIView和Coco2d-x场景之间的相互跳转切换(转)(uiview和calayer关系)

①第一个图片所对应的MyViewController

- (void)viewDidLoad{ [superviewDidLoad]; // Do any additional setup after loading the view. UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(0, , , ); [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 { [superviewDidLoad]; //添加一个button UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(0, , , ); [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_COMPONENT preserveBackbuffer: NO sharegroup: nil multiSampling: NO numberOfSamples:0 ]; [__glView setFrame:CGRectMake(0, 0, , )]; [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 { returnUIInterfaceOrientationIsPortrait( interfaceOrientation );} // For ios6, use supportedInterfaceOrientations & shouldAutorotate instead- (NSUInteger) supportedInterfaceOrientations{#ifdef __IPHONE_6_0 returnUIInterfaceOrientationMaskPortrait;#endif} - (BOOL) shouldAutorotate { returnYES;}

好咯,主要的代码都已经贴出来了。

(5)下面再具体的看看其中关键的部分:

RootViewController

①我们创键了EAGLView的实例,作为subview添加到当前的view。而且设置它的rect为(0,0,,)

那么我们可以在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(); returnYES;}

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];}下面为了有需要的同志,给出navigation实现的代码下载链接:点击打开链接下载链接:

cocos2dx游戏 方块向前冲 开源 方块向前冲是我第一个游戏项目,也算是一个小小的尝试吧,在开发过程中借鉴了很多博客和开源项目,感觉自己各方面都有很大提升,现在决定把这

[置顶] Cocos2d-x《雷电大战》(3)-子弹无限发射 林炳文Evankaka原创作品。转载请注明出处

基于cocoStudio和BMfont的艺术字体制作 我们在制作游戏的过程中经常要使用各式各样的艺术字体,这些字体让我们的游戏看起来更加美观更加的萌(--!)但是很多的新手都不知道这些字体是

标签: uiview和calayer关系

本文链接地址:https://www.jiuchutong.com/biancheng/368946.html 转载请保留说明!

上一篇:libpomelo使用(libproj.so)

下一篇:cocos2dx游戏 方块向前冲 开源(cocos2dx开发的游戏)

  • 增值税纳税人是委托方还是受托方
  • 增值税普票税额怎么算出来的
  • 技术服务费怎么算成本
  • 个税里的累计专票是什么
  • 当月开票没有收到钱
  • 小规模纳税人进项
  • 支付宝怎么开个人增值税发票
  • 教育用地转让缴哪些税
  • 物流公司进项发票分录
  • 小规模纳税人开专票税率是1%还是3%
  • 企业代扣职工个人缴纳的五险一金
  • 印花税账务处理
  • 开发间接费用是指
  • 购买汽车怎么抵扣增值税
  • 税交多了退税走到哪一流程了问谁
  • 以前年度不合规发票怎么处理
  • 设备租赁公司购进设备怎么做账
  • 个人独资企业个税税率表2023
  • 生产型增值税与消费型增值税的区别在于是否允许企业
  • 劳务费发票和建筑劳务费发票
  • 营改增后还要交企业所得税吗
  • 公司年末补缴企业所得税需要提供什么资料?
  • 物业公司停车费怎么开票
  • 生物性资产是什么
  • 资产负债表里应交税费里都包含什么
  • 外购固定资产进项税
  • 金融企业贷款利息的账务处理
  • 红字专用发票信息表
  • 购买原材料的运输费计入什么科目
  • 其他应收款的二级科目有备用金和老板名可以吗?
  • 开机后网络连接很慢
  • u启动pe装机工具怎么重装系统
  • macOS Big Sur 11.3 开发预览版 Beta正式更新
  • Python之ImportError: DLL load failed: 找不到指定的模块解决方案
  • 微信小程序webview支付
  • 怎么检测pvc中含量
  • 公章盖的字不完整有效吗
  • 路由器无线设置模式哪个更快
  • 所得税预缴申报表资产总额怎么填
  • sqlserver2008实例配置
  • 设备出租当月要确认收入吗
  • 什么是注册资本
  • 福利费专票必须抵扣再转出吗
  • 企业所得税的税基是什么
  • 差额征税问题
  • 工资扣员工的罚款入什么科目
  • 基本户转到一般户用途写什么
  • 企业营业利润率怎么算
  • 自创商誉是否需要纳税
  • 什么是库存现金限额
  • 结账没有原始凭证
  • mysql中sum的用法
  • 5个经常被忽略的成语
  • 安装solaris11
  • vpengine.exe进程
  • win8.1系统升级win10
  • xp系统怎么修改图标和文字大小
  • host文件内容
  • linux urb
  • RedHat Linux5.5下Oracle 11g安装图解教程
  • E: Encountered a section with no Package: header错误解决方法
  • win7系统怎样
  • cocos2dx内存管理
  • h5 nodejs
  • listview安卓
  • unity进度条控制动画进度
  • jQuery实现textarea自动增长宽高的方法
  • android list
  • javascript教程完整版
  • 深入理解javascript特性
  • unity3d Human skin real time rendering with blood and water drop effect真实模拟人皮实时渲染之血液和水珠掉落效果
  • jquery3.3.1
  • python魔法方法有啥用
  • python程序解读举例
  • python shutil
  • 国家税务局广东省电子税务总局手机版
  • 北京朝阳地税局电话号码
  • 辽宁社保网上申报流程图
  • 江苏国税,地税怎么交
  • 企业以自有物业为单位
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设