|
博客:金属星球
1、创建一个继承于GLKViewController类的新类,比如HelloGLKitViewController
2、编辑文件HelloGLKitViewController.m,增加以下内容
@interface HelloGLKitViewController (){float _curRed;
BOOL _increasing;
}@property(strong, nonatomic) EAGLContext *context;
@end
@implementation HelloGLKitViewController
@synthesize context = _context;
并增加
- -(void)viewDidLoad
- {[super viewDidLoad];
-
- self.context =[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
-
- if(!self.context){
- NSLog(@"Failed to create ES context");
- }
-
- GLKView *view =(GLKView *)self.view;
- view.context = self.context;
- }
-
- -(void)viewDidUnload
- {[super viewDidUnload];
-
- if([EAGLContext currentContext]== self.context){[EAGLContext setCurrentContext:nil];
- }
- self.context =nil;
- }#pragma mark - GLKViewDelegate
-
- -(void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
-
- glClearColor(_curRed, 0.0, 0.0, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
-
- }
-
- #pragma mark - GLKViewControllerDelegate
-
- -(void)update {if(_increasing){
- _curRed +=1.0* self.timeSinceLastUpdate;
- }else{
- _curRed -=1.0* self.timeSinceLastUpdate;
- }if(_curRed >=1.0){
- _curRed =1.0;
- _increasing =NO;
- }if(_curRed <=0.0){
- _curRed =0.0;
- _increasing =YES;
- }}
-
- // 暂停功能
- -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event {
- self.paused =!self.paused;
- }
复制代码
3、在AppDelegate.m文件中删除 didFinishLaunchingWithOptions 函数中的内容,只留下 return YES;
4、然后创建一个新的Storyboard文件,命名为:MainStoryboard.storyboard打开这个文件,拖拽一个 View Controller 到空白处,选择这个控件在 Identity Inspector 中修改 class 为我们之前创建好的HelloGLKitViewController再点击控件内部的空白处,同样的方法设置 class 为 GLKView 。
5、下一步需要将这个故事板设定为启动项打开 HelloGLKit-Info.plist 文件,右键添加一行,下拉列表中选择 Main storyboard file base name 在后面的格子中填上故事板的文件名 MainStoryboard.
|
|