gpt4 book ai didi

ios - 无法在 UIView 中将自定义类设置为 GLKView

转载 作者:行者123 更新时间:2023-12-01 20:19:59 24 4
gpt4 key购买 nike

我正在学习适用于 iOS 的 OpenGL ES 并在线学习 Ray Wenderlich 的教程。他做的第一件事是使用 OpenGL ES 2.0 将 View Controller 中的 View 设置为不同的颜色。所以我创建了一个名为 OpenGLView 的自定义类,它负责改变屏幕的颜色。然后在主 Storyboard中,我删除了 View Controller 中的原始 View ,并将其替换为 GLKView,并将其类设置为 OpenGLView。当我运行该项目时,一切正常。但现在我试图做同样的事情,除了 GLKView 在 UIView 内。像下面这样

enter image description here

当我在主 Storyboard 中将 GL View 的类设置为我的自定义类 OpenGlView 时,除了该类之外它没有。当我运行这个项目时,我看到的只是一个白屏。

如何将 GL View 设置为 OpenGLView 类?

编辑:

我相信它与应用程序委托(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;
}

最佳答案

您的代码在应用程序委托(delegate)中创建一个 glView 并将其添加为根窗口的 subview ;之后,通过 Storyboard 实例化 View Controller 并将其 View 添加到堆栈中。最后一个 View 是您看到的白色 View ,它覆盖了 glView。
为了更好地理解这种层次结构,您可以使用 Xcode Debug View Hierarchy 功能。
要解决您的问题,您可以删除 View Controller 或将 glView 作为 View Controller 的 subview 移动。

编辑:
如果您通过 Storyboard 创建 OpenGLView,initWithCoder使用而不是initWithFrame .将以下方法添加到 OpenGLView.m:

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

关于ios - 无法在 UIView 中将自定义类设置为 GLKView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35873388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com