cocos2d-x 3.2 调用原生iOS方法
1. UINavigationController
cocos2d-x 启动后会调用AppController:
// main.m
#importint main(int argc, char *argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, @"AppController");
}
}
在AppController完成启动的过程中,创建了RootViewController,绑定GLView:
// Use RootViewController manage CCEAGLView
_viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
_viewController.wantsFullScreenLayout = YES;
_viewController.view = eaglView;
// 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];
}
可以创建一个UINavigationController作为_window的rootViewController,将_viewController用push的方式放到nav中。如需调用原生的UIViewController,使用一样的方法push/pop即可。
尚未解决的问题是:如果我只是在GLView的界面上增加一个原生API的显示控件,两个view是层叠的效果,上层(即原生显示控件)部分透明地改在GLView上,这个方法需要改进。测试结果是,上层只有一个加了局部白色的UIDatePicker时,余下的全是黑色。
2. RootViewController
我异想天开地自定义了一个UIView,就是之前白底黑字的UIDatePicker,通过addSubView的方式直接将其加到RootViewController的view上。确实可以看见了,但是,UIDatePicker根本就不相应点触,而这个UIView就是个透明盖子,底下的GLView上的点触丝毫没受影响。
然后,我用UIViewController替换原来的UIView,把白底黑子的UIDatePicker加到这个VC的view上,Picker可以转了,但是GLView还是继续相应点触。加到_window就屏蔽掉了GLView的点触。
当当当当!正经的来了!文字后面再补吧。点我链接
// AppController.mm
+ (void) showUIViewController:(UIViewController *)controller
{
if (IOS_8_OR_LATER)
{
controller.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
else
{
s_pCurrentViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
}
[s_pCurrentViewController presentViewController:controller animated:YES completion:{}];
}
调用的地方
DatePicker *dp = [[DatePicker alloc] init];
[AppController showUIViewController:dp];
// DatePicker.h
#import [Foundation/Foundation.h]
#import [UIKit/UIKit.h]
@interface DatePicker : UIViewController
@property (nonatomic, retain) UIView *bg;
@property (nonatomic, retain) UIDatePicker *dp;
@property (nonatomic, retain) UIButton *bt;
@end
// DatePicker.mm
#import "DatePicker.h"
#include "AppController.h"
@implementation DatePicker
@synthesize bg;
@synthesize dp;
@synthesize bt;
- (void) viewDidLoad
{
bg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 200)];
[bg setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:bg];
dp = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 300, 200)];
[dp setDatePickerMode:UIDatePickerModeDate];
[self.view addSubview:dp];
bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
bt.frame = CGRectMake(10 , 10, 75, 44);
[bt setTitle:@"完成" forState:UIControlStateNormal];
[self.view addSubview:bt];
}
- (void) dealloc
{
[dp release];
[bt release];//[UIButton buttonWithType:UIButtonTypeCustom]之类的不需要release的
[bg release];
[super dealloc];
}
@end