【问题标题】:Nesting GLKView into UIViewController将 GLKView 嵌套到 UIViewController
【发布时间】:2012-04-03 01:26:17
【问题描述】:

我正在尝试在 UIViewController 中使用 GLKView,我的代码如下所示

CustomViewController.h

#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>

@interface CustomViewController : UIViewController
{
    NSString * name;
}

@property NSString * name;

CustomViewController.m

#import "CustomViewController.h"

@interface CustomViewController ()

@end

@implementation CustomViewController
@synthesize name;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    CGRect mainScreen = [[UIScreen mainScreen] bounds];
    GLKView * viewGL = [[GLKView alloc] initWithFrame:CGRectMake(mainScreen.size.width / 2, mainScreen.size.height / 2, 50, 50)];
    viewGL.context = context;
    [self.view addSubview:viewGL];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

如何定义 GLKView 的绘制/渲染方法?我在哪里可以初始化 OpenGL?有什么建议吗?

【问题讨论】:

    标签: ios glkit


    【解决方案1】:

    在您的viewDidLoad 中初始化 OpenGL,就像您目前正在做的那样。

    看看将您的视图控制器注册为GLKView's delegate。每当需要重绘时,都会调用委托的 glkView:(GLKView *)view drawInRect: 方法。

    This tutorial may help.

    【讨论】:

    • 我去看看教程,我还需要检查代理是如何工作的
    • 您可能还想看看使用GLKViewController 而不是 UIViewController。它实现了一个渲染循环,因此您可以每秒多次定期更新您的 GLKView。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多