cocos2d-x 3.2 調(diào)用原生iOS方法

1. UINavigationController

cocos2d-x 啟動(dòng)后會(huì)調(diào)用AppController:

// main.m

#importint main(int argc, char *argv[]) {

@autoreleasepool {

return UIApplicationMain(argc, argv, nil, @"AppController");

}

}

在AppController完成啟動(dòng)的過(guò)程中,創(chuàng)建了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];

}

可以創(chuàng)建一個(gè)UINavigationController作為_window的rootViewController,將_viewController用push的方式放到nav中。如需調(diào)用原生的UIViewController,使用一樣的方法push/pop即可。

尚未解決的問(wèn)題是:如果我只是在GLView的界面上增加一個(gè)原生API的顯示控件,兩個(gè)view是層疊的效果,上層(即原生顯示控件)部分透明地改在GLView上,這個(gè)方法需要改進(jìn)。測(cè)試結(jié)果是,上層只有一個(gè)加了局部白色的UIDatePicker時(shí),余下的全是黑色。

2. RootViewController

我異想天開地自定義了一個(gè)UIView,就是之前白底黑字的UIDatePicker,通過(guò)addSubView的方式直接將其加到RootViewController的view上。確實(shí)可以看見了,但是,UIDatePicker根本就不相應(yīng)點(diǎn)觸,而這個(gè)UIView就是個(gè)透明蓋子,底下的GLView上的點(diǎn)觸絲毫沒受影響。

然后,我用UIViewController替換原來(lái)的UIView,把白底黑子的UIDatePicker加到這個(gè)VC的view上,Picker可以轉(zhuǎn)了,但是GLView還是繼續(xù)相應(yīng)點(diǎn)觸。加到_window就屏蔽掉了GLView的點(diǎn)觸。

當(dāng)當(dāng)當(dāng)當(dāng)!正經(jīng)的來(lái)了!文字后面再補(bǔ)吧。點(diǎn)我鏈接

// 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:{}];

}

調(diào)用的地方

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容