0

我正在尝试将 a 嵌套GLKViewUIViewXIB 中。基本上按照这里的步骤: 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];
}

这会导致viewDidLoadCustomOpenGLController调用,它执行以下操作:

自定义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);
}

问题是代表glkViewControllerUpdateglkViewdrawInRect 没有被调用。

如果我添加CustomOpenGLController为子视图(而不是 setView 调用),那么glkViewdrawInRect 会被调用一次。如果单独CustomOpenGLController用作视图,则一切正常。

但是我需要CustomOpenGLController将其嵌入普通视图控制器中。

更新

如果我将viewDidLoadfor MainViewController.m修改为以下内容,那么它可以工作,但它只渲染 1 帧然后停止调用glkViewdrawInRect:

- (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永远不会被调用。

4

1 回答 1

1

You're setting CustomOpenGLController as a child ViewController, yet you never call addChildViewController in your MainViewController viewDidLoad. Your problem might come from this

于 2013-11-04T14:58:41.967 回答