将GLKView嵌套到UIViewController中

Joh*_*Ace 7 ios glkit

我正在尝试在UIViewController中使用GLKView,我的代码看起来像这样

CustomViewController.h

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

@interface CustomViewController : UIViewController
{
    NSString * name;
}

@property NSString * name;
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

Sco*_*man 9

viewDidLoad正如您目前所做的那样,初始化OpenGL .

看一下将视图控制器注册为GLKView的委托.glkView:(GLKView *)view drawInRect:只要需要重绘,就会调用委托的方法.

本教程可能有所帮助.