我想使用 OpenGL 做一些简单的图像处理,所以我开始使用 GLKView
。因为我不需要每隔几秒刷新 View ,所以我没有使用 GLKViewController
而是使用普通的 UIViewController
子类。
我的问题是,我是简单地将 viewController 的 View 作为 GLKView
还是添加 GLKView
作为 View Controller View 的 subview 。因为我也在 View 中添加了一个 UISlider
,所以我认为后者似乎更好,但我不确定。在某些情况下,我还需要在 GLKView 上调用 setNeedsDisplay
。
最佳答案
对于您的渲染,您确实应该在 GLKViewController 中使用 GLKView。如果您担心不需要一直刷新,请在 GLKViewController 中使用 self.paused = YES,这将停止渲染循环,当您需要再次渲染时,只需执行 self.paused = NO。
如果您在另一个 View 中有 glkview,您应该设置它的包含。在你的情况下,你应该有一个普通的 UIView 和一个普通的 UIViewController,然后将 UISlider 和你的 GLKViewController(带有 GLKView)添加到它。
完成后,您可以在父 Controller 中执行普通 View 操作,而您的 opengl 操作就是您的 glk Controller 。
执行此操作的一个简单示例,已设置包含 UISlider 的父项:
在父级的自定义 UIViewController 中
@interface ParentViewController () {
...
UISlider *_slider; // this is your slider
CustomGLKViewController *_myGlkViewController;
}
然后在 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;
但这只是一个帮助您入门的简单示例,您真的应该去阅读关于 ios5 的 UIViewController 包含的苹果文档和 GLKit 的文档
关于iphone - 将 GLKView 与 UIViewController 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12100689/