使用 OpenGL 进行 Ios UIButton 子类化
是否可以创建 UIButton 的子类来在 OpenGL ES 中进行按钮绘制?到目前为止,我有一个仅使用 OpenGL ES 的游戏应用程序,出于性能和美观的原因,我不想在此基础上添加复合 UIkit 元素。话又说回来,我真的不想对矩形实现触摸的命中测试,因为要像苹果对内部和外部矩形以及各种状态转换所做的那样正确地进行测试需要相当多的代码。我真正想要的是让 UIButton 执行所有触摸状态更改,但不进行绘图,同时允许我使用 OpenGL 以及所有其余绘图代码自行在各种状态下绘制按钮。有关如何进行的任何建议或代码示例?
Is it possible to create subclasses of UIButton which do the button drawing in OpenGL ES? I have a game app that uses only OpenGL ES so far and I do not want to add composited UIkit elements on top of this both for performance and aesthetic reasons. Then again I do not really want to implement hit testing of touches against rects because to do it correctly as apple does with inner and outer rects and various state transitions is quite a lot of code. What I really want is to have the UIButton do all the touch state changes but have it do no drawing, while allowing me to draw the buttons myself in various states using OpenGL along with all the rest of my drawing code. Any suggestions or code examples of how to proceed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现这个问题的一个答案是使用 UIButton 但使用自定义样式。然后,如果您没有为按钮状态设置实际图像,它根本不会绘制任何内容,但仍然可以充当按钮,当在您提供的矩形中检测到所需的操作时回调您。通过这种方式,您可以将 GLES 纹理的区域作为按钮激活。当触摸或释放按钮(无论是否有动作)时,您都可以获得回调,并且如果需要,您可以自己更新纹理以响应这些回调。但最终我没有这样做,只是按照正常的 UIKit 使用方式使用带有图像的自定义按钮。我没有发现在 OpenGL ES 之上使用 UIKit 时我的应用程序有任何过度减慢的情况。
I found that one answer to this question would be to make use of UIButton but use Custom style. Then if you do not set the actual images for the button states it does not draw anything at all, but still functions as a button by calling you back when the desired action is detected in the rectangle that you supply. This way you can make regions of an GLES texture active as buttons. You can get callbacks when the button is touched or released with or without action and you can update your texture yourself in response to these if desired. However in the end I did not do this, but just used the Custom buttons with images as per normal UIKit use. I did not find any excess slow down of my app using UIKit on top of OpenGL ES.