Search code examples
iosiphoneopengl-esuikituiresponder

Can a OpenGL game engine create it's own UIResponder subclass similar to Sprite Kit does for SKNode?


It looks like UIResponder is interwoven with UIView. Yet, Sprite Kit has the SKNode class which inherits from UIResponder so it can handle touch events.

If you create a node-based game engine yourself: Can you create your own UIResponder subclass so your objects rendered by OpenGL can respond to touch events similar to a UIView hierarchy could?

And how are the touch events coming into the UIResponder subclass?


Solution

  • I don't believe this is possible in the way that SKNode has been implemented. There is no public API for inserting custom UIResponder subclasses into the responder chain.

    See ricksters answer on details how to insert a UIResponder subclass into the responder chain correctly. Be sure to override - (UIResponder *)nextResponder in the child nodes to return your custom view class, so that the responder chain is still correct.

    One possibility is to create a UIControl for each of your GL objects, and keep its position in sync. By adding these controls as subviews of your top level GL view, they will be inserted into the responder chain and receive touches. Another possibility is to handle touches in your top level view, and perform hit testing there to decide which GL node intersects with the touch.