将GLKView与UIViewController一起使用 [英] Using GLKView with a UIViewController

查看:198
本文介绍了将GLKView与UIViewController一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用OpenGL进行一些简单的图像处理,因此我从GLKView开始.由于不需要每隔几秒钟刷新一次视图,因此我没有使用GLKViewController而是使用了普通的UIViewController子类.

I wanted to use OpenGL to do some simple Image processing, so I started off with GLKView. Since I don't need to refresh the view every few seconds, I didn't use the GLKViewController and instead used a normal UIViewController subclass.

我的问题是,我只是将viewController的视图制作为GLKView还是将GLKView添加为视图控制器的视图的子视图.由于我也在视图中添加了UISlider,因此我认为后者似乎更好,但我不确定.在某些情况下,我还需要在GLKView上调用setNeedsDisplay.

My question is that do I simply make the viewController's view as a GLKView or do I add the GLKView as a subview of the view controller's view. Since I'm adding a UISlider to the view as well, I think the latter seems better, but I'm not sure. I also need to call setNeedsDisplay on the GLKView on certain occasions.

推荐答案

对于渲染,您实际上应该在GLKViewController中使用GLKView.如果您担心不需要一直刷新,请在GLKViewController中使用self.paused = YES,这将停止渲染循环,而当您需要再次渲染时,只需执行self.paused = NO.

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.

如果您在另一个视图中包含glkview,则应将其设置为包含.在您的情况下,您应该有一个带有普通UIViewController的普通UIView,然后向其中添加UISlider和GLKViewController(带有GLKView).

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.

完成此操作后,您可以在父控制器中进行常规视图操作,而opengl则是glk控制器.

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

执行此操作的一个简单示例,设置了您的父级,其中包含UISlider:

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

在父级的自定义UIViewController内

inside the custom UIViewController for the parent

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

然后在viewDidLoad中:

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;

但这只是一个帮助您入门的简单示例,您确实应该阅读ios5的UIViewController容器中的Apple文档和GLKit的文档

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

这篇关于将GLKView与UIViewController一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆