Add UIViewController in cocos2d(在 cocos2d 中添加 UIViewController)
问题描述
我想在 cocos2d 项目中显示 UIViewController,所以我在我的 CCLayer 类中这样做了:
i want to display a UIViewController in a cocos2d project, so i have do this in my CCLayer class:
-(void)displayMainMenu {
CGSize screenSize = [CCDirector sharedDirector].winSize;
[CCMenuItemFont setFontName:@"Marker Felt"];
[CCMenuItemFont setFontSize:26];
CCMenuItemFont *openViewC = [CCMenuItemFont itemWithString:@"Open View" target:self selector:@selector(loadMyViewController)];
mainMenu = [CCMenu menuWithItems:openViewC, nil];
[self addChild:mainMenu z:0];
}
-(void) loadMyViewController{
//Add the tableview when the transition is done
myView = [[MyViewController alloc] init];
UIView *viewHost = hostView.view;
[[[CCDirector sharedDirector] view] addSubview:viewHost];
}
然后在我的 ViewController 中返回我的 CCLayer 我这样做:
and then in my ViewController to return to my CCLayer i do this:
- (IBAction)exitAction:(id)sender
{
[self.view removeFromSuperview];
[[CCDirector sharedDirector] pushScene: [MainMenu scene]]; //i need it or not?
}
一切正常,我使用 cocos2d v2.0,但我想知道是否有更好的方法可以在 cocos2d 场景中添加 UIViewController,谢谢!
and all work, i use cocos2d v2.0, but i want know if there is a better way to add a UIViewController in a cocos2d scene, thanks!
推荐答案
由于cocos2d 2.0中的director是UIViewController的子类,作为加载在AppDelegate中的导航控制器的根,其实有一个非常简单的方法可以利用UIKit 动画,如果你喜欢的话.它还使场景完好无损,而无需真正担心内存管理.您可以将您的 -loadMyViewController 方法更改为:
Since the director in cocos2d 2.0 is a subclass of UIViewController as the root of a navigation controller loaded in the AppDelegate, there is actually a very simple way that can utilize UIKit animations, if you prefer that. It also leaves the scene intact without really worrying about memory management. You could alter your -loadMyViewController method to this:
- (void)loadMyViewController {
myView = [[MyViewController alloc] init];
AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
[app.navController pushViewController:myView animated:YES];
[CCDirector sharedDirector].pause;
}
然后在想要返回 cocos2d 场景时简单地恢复 director 并调用 [app.navController popViewController...].或者,您可以在推送视图控制器而不是暂停时使用 stopAnimation 方法,类似于 cocos2d 处理应用进入后台的方式.
And then simply resume the director and call [app.navController popViewController...] when you want to return to the cocos2d scene. Alternatively, you could use the stopAnimation method when you push your view controller instead of pause, similar to how cocos2d handles the app entering the background.
这篇关于在 cocos2d 中添加 UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 cocos2d 中添加 UIViewController
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01