我正在尝试将 a 嵌套GLKView
在UIView
XIB 中。基本上按照这里的步骤:
Nested GLKView and GLKViewController
我的CustomOpenGLController.xib只是一个GLKView
.
我的MainViewController.xib有一个GLKView
子视图。
在我的MainViewController.xib我有GLKView
一个插座:
@property (weak, nonatomic) IBOutlet GLKView *theSubView;
然后在MainViewController.m我执行以下操作:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"viewDidLoad");
subviewController = [[CustomOpenGLController alloc] initWithNibName:@"CustomOpenGLController" bundle:nil];
subviewController.view.frame = _theSubView.frame;
[subviewController setView:self.theSubView];
[self.theSubView setNeedsDisplay];
[subviewController didMoveToParentViewController:self];
}
这会导致viewDidLoad
被CustomOpenGLController
调用,它执行以下操作:
自定义OpenGLController.h
@interface CustomOpenGLController : GLKViewController <GLKViewControllerDelegate, GLKViewDelegate>
{
@private
GLKBaseEffect *effect;
}
#pragma mark GLKViewControllerDelegate
- (void)glkViewControllerUpdate:(GLKViewController *)controller;
#pragma mark GLKViewDelegate
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect;
@end
自定义OpenGLController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"viewDidLoad");
EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
GLKView *glkView = (GLKView *)self.view;
glkView.delegate = self;
glkView.context = aContext;
glkView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
glkView.drawableDepthFormat = GLKViewDrawableDepthFormat16;
glkView.drawableMultisample = GLKViewDrawableMultisample4X;
self.delegate = self;
self.preferredFramesPerSecond = 30;
effect = [[GLKBaseEffect alloc] init];
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
问题是代表glkViewControllerUpdate
和glkView
drawInRect 没有被调用。
如果我添加CustomOpenGLController
为子视图(而不是 setView 调用),那么glkView
drawInRect 会被调用一次。如果单独CustomOpenGLController
用作视图,则一切正常。
但是我需要CustomOpenGLController
将其嵌入普通视图控制器中。
更新
如果我将viewDidLoad
for MainViewController.m修改为以下内容,那么它可以工作,但它只渲染 1 帧然后停止调用glkView
drawInRect:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"viewDidLoad ControllerView");
subviewController = [[ControllerOpenGLViewController alloc] initWithNibName:@"ControllerOpenGLViewController" bundle:nil];
subviewController.view.frame = _theSubView.frame;
subviewController.view.opaque = NO;
subviewController.view.backgroundColor = [UIColor clearColor];
[self.view addSubview:subviewController.view];
[subviewController.view setNeedsDisplay];
NSLog(@"Done viewDidLoad ControllerView");
}
这奇怪地渲染了 1 帧然后停止。也glkViewControllerUpdate
永远不会被调用。