将GLKView嵌套到UIViewController中 [英] Nesting GLKView into UIViewController
问题描述
我正在尝试在UIViewController中使用GLKView,我的代码看起来像这样
I'm trying to use GLKView in UIViewController, my code looks like this
CustomViewController.h
CustomViewController.h
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>
@interface CustomViewController : UIViewController
{
NSString * name;
}
@property NSString * name;
CustomViewController.m
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?有什么建议吗?
How can I define the draw/render method of GLKView? and where can I init OpenGL? Any suggestions?
推荐答案
在 viewDidLoad
中初始化OpenGL,就像你一样'目前正在做。
Initialize OpenGL in your viewDidLoad
, just as you're currently doing.
看一下将视图控制器注册为GLKView的代表。每当需要重绘时,代理的 glkView:(GLKView *)视图drawInRect:
方法将被调用。
Take a look at registering your view controller as the GLKView's delegate. The delegate's glkView:(GLKView *)view drawInRect:
method will be invoked whenever a redraw is required.
< a href =http://www.raywenderlich.com/5223/beginning-opengl-es-2-0-with-glkit-part-1>本教程可能有所帮助。
这篇关于将GLKView嵌套到UIViewController中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!