iOS:在 EAGLView / GLKView 之上创建覆盖层

发布于 2024-12-27 17:16:35 字数 1135 浏览 1 评论 0原文

我正在尝试在 GLKView (实际上是 EAGLView)之上创建一个覆盖层。我知道性能影响,但在我的情况下这不是问题,因为场景在后台暂停,它只需要保持可见即可。

我创建了一个名为 ReaderView 的自定义 UIView,其唯一的自定义代码如下:

-(CALayer*)layer {
CATextLayer *textLayer = [[CATextLayer alloc] init];

// Layer settings.
[textLayer setCornerRadius:5.0f];

// Text settings.
[textLayer setFont:CGFontCreateWithFontName((CFStringRef)READING_FONT)];
[textLayer setFontSize:READING_FONT_SIZE];
[textLayer setAlignmentMode:kCAAlignmentJustified];
[textLayer setWrapped:YES];

return textLayer;
}

然后我在 GLKViewController 中调用了以下内容:

-(void)onMyCustomEvent {
if (_readerView==nil) {
    CGRect frame = [[self view] frame];
    frame.size.width *= 0.8f;
    frame.size.height *= 0.8f;
    _readerView=[[ReaderView alloc] initWithFrame:frame];
    [_readerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
}
[_readerView setText:[node content]];
[[self view] addSubview:_readerView];
}

NSLog已经证明这个方法被调用并且阅读器视图被初始化。但是 GLKView 顶部没有显示任何内容。

知道为什么这不起作用吗?

I'm trying to create an overlay on top of a GLKView (effectively an EAGLView). I'm aware of the performance impact, but in my situation that's not a problem, since the scene is paused in the background, it merely needs to remain visible.

I've created a custom UIView called ReaderView whose only custom code is the following:

-(CALayer*)layer {
CATextLayer *textLayer = [[CATextLayer alloc] init];

// Layer settings.
[textLayer setCornerRadius:5.0f];

// Text settings.
[textLayer setFont:CGFontCreateWithFontName((CFStringRef)READING_FONT)];
[textLayer setFontSize:READING_FONT_SIZE];
[textLayer setAlignmentMode:kCAAlignmentJustified];
[textLayer setWrapped:YES];

return textLayer;
}

I've then called the following in a GLKViewController:

-(void)onMyCustomEvent {
if (_readerView==nil) {
    CGRect frame = [[self view] frame];
    frame.size.width *= 0.8f;
    frame.size.height *= 0.8f;
    _readerView=[[ReaderView alloc] initWithFrame:frame];
    [_readerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
}
[_readerView setText:[node content]];
[[self view] addSubview:_readerView];
}

NSLog has proven this method gets called and the reader view gets initialized. However nothing displays on top of the GLKView.

Any idea why this doesn't work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南冥有猫 2025-01-03 17:16:35

由于您将其添加为子视图,因此您应该使用bounds属性而不是父视图中的frame属性,如下所示:

CGRect frame = self.view.bounds;

如果这不能解决问题,请尝试将_readerView的背景颜色设置为值得注意的颜色以确保问题不在于缺少内容,如下所示:

_readerView.backgroundColor = [UIColor redColor];

最后,如果这些都不能解决问题,那么可能直接向 EAGL 视图添加子视图就是问题所在。在这种情况下,请创建一个容器视图并向其中添加 EAGL 视图和 _readerView。

Since you're adding it as a subview, you should use the bounds property instead of the frame property from the parent view, like this:

CGRect frame = self.view.bounds;

If that doesn't fix it, try setting the background color of the _readerView to something noticable to ensure that the problem isn't the content missing, like this:

_readerView.backgroundColor = [UIColor redColor];

Finally, if none of that solves it, then maybe directly adding subviews to an EAGL view is the problem. In that case, create a container view and add your EAGL view and your _readerView to that instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文