无法在UIView中将自定义类设置为GLKView [英] Cannot set custom class to GLKView inside UIView

查看:194
本文介绍了无法在UIView中将自定义类设置为GLKView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习iOS的OpenGL ES,并遵循Ray Wenderlich的在线教程.他要做的第一件事是使用OpenGL ES 2.0将视图控制器中的视图设置为其他颜色.因此,我创建了一个名为OpenGLView的自定义类,该类负责更改屏幕的颜色.然后在主故事板上,我删除了视图控制器中的原始视图,并用GLKView替换了它,并将其类设置为OpenGLView.当我运行项目时,一切正常.但是现在我试图做同样的事情,除了GLKView位于UIView内.像下面的

I am learning OpenGL ES for iOS and following Ray Wenderlich's tutorial online. The first thing he does is set the view in a view controller to a different color using OpenGL ES 2.0. So I created the custom class called OpenGLView which is responsible for changing the color of the screen. Then in the main story board I deleted the original view in the view controller and replaced it with a GLKView and set its class to OpenGLView. When I run the project everything works fine. But now I was trying to do the same, except the GLKView is inside a UIView. Like the following

当我将GL View的类设置为主故事板上的自定义类OpenGlView时,该类除外.当我运行该项目时,我看到的只是一个白屏.

When I set the class of the GL View to my custom class OpenGlView in the main storyboard it does not except the class. And when I run the project all I see is a white screen.

如何将GL View设置为OpenGLView类?

How can I set the GL View to the OpenGLView class?

我相信它与应用程序委托中的代码有关.

I believe it has something do with the code in the app delegate.

@interface AppDelegate : UIResponder <UIApplicationDelegate>{

OpenGLView* _glView;
}
 @property (strong, nonatomic) UIWindow *window;
 @property (nonatomic, retain) IBOutlet OpenGLView *glView;

 @end

//In the implementation
@synthesize glView=_glView;

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    self.glView = [[OpenGLView alloc] initWithFrame:screenBounds];
    [self.window addSubview:_glView];
    return YES;
}

推荐答案

您的代码在应用程序委托中创建glView,并将其添加为根窗口的子视图;然后,通过情节提要实例化视图控制器,并将其视图添加到堆栈中.最后一个视图是您看到的白色视图,它涵盖了glView. 为了更好地理解这种层次结构,可以使用Xcode Debug View Hierarchy功能. 要解决您的问题,您可以删除视图控制器,也可以将glView移为视图控制器的子视图.

Your code creates a glView in the app delegate as adds it as subview of the root window; afterwards a view controller is instantiated through the storyboard and its view added to the stack. This last view is the white view you see and it covers the glView. To better understand such hierarchy you can use the Xcode Debug View Hierarchy functionality. To solve your issue you can either remove the view controller or move your glView as subview of the view controller.

如果通过情节提要板创建OpenGLView,则将使用initWithCoder而不是initWithFrame.将以下方法添加到OpenGLView.m:

If you create the OpenGLView through a storyboard, initWithCoder is used and not initWithFrame. Add the following method to OpenGLView.m:

- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super initWithCoder:decoder];
    if (self) {
        [self setupLayer];
        [self setupContext];
        [self setupRenderBuffer];
        [self setupFrameBuffer];
        [self render];
    }
    return self;
}

这篇关于无法在UIView中将自定义类设置为GLKView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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