iOS:在 EAGLView / GLKView 之上创建覆盖层
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您将其添加为子视图,因此您应该使用bounds属性而不是父视图中的frame属性,如下所示:
如果这不能解决问题,请尝试将_readerView的背景颜色设置为值得注意的颜色以确保问题不在于缺少内容,如下所示:
最后,如果这些都不能解决问题,那么可能直接向 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:
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:
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.