Using GLKView with a UIViewController

拜拜、爱过 提交于 2019-12-04 16:40:17

For your rendering, you should really be using GLKView inside a GLKViewController. if you're worried about not needing to refresh all the time, use self.paused = YES inside your GLKViewController, this will stop the rendering loop, and when you need to render again, simply do self.paused = NO.

if you're having the glkview inside another view, you should set it up with containment. in your case, you should have a normal UIView with a normal UIViewController, then add the UISlider and your GLKViewController (with the GLKView) to that.

After this is done, you can do your normal view stuff in your parent controller, and your opengl stuff is your glk controller.

A simple example to do this, having setup your parent, which contains the UISlider:

inside the custom UIViewController for the parent

@interface ParentViewController () {
    ...
    UISlider *_slider; // this is your slider
    CustomGLKViewController *_myGlkViewController;
}

then inside viewDidLoad:

// assuming you're using a storyboard
UIStoryboard *myStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                      bundle:[NSBundle mainBundle]];


// setup the opengl controller
// first get an instance from storyboard
_myGlkViewController = [myStoryboard instantiateViewControllerWithIdentifier:@"idForGlkViewController"];
// then add the glkview as the subview of the parent view
[self.view addSubview:_myGlkViewController.view];
// add the glkViewController as the child of self
[self addChildViewController:_myGlkViewController];
[_myGlkViewController didMoveToParentViewController:self];

// if you want to set the glkView to the same size as the parent view, 
// or you can do stuff like this inside myGlkViewController
_myGlkViewController.view.frame = self.view.bounds;

but this is just a simple example to help you get started, you should really go read the apple docs on UIViewController containment for ios5 and the docs for GLKit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!