Tutorial :Detecting touches on a NSOpenGLView



Question:

I have added an NSOpenGLView to my MainMenu.xib file, and have implemented drawRect to draw to it, this works great, so far so good.

How do I detect touches on the screen? I imagine I could perhaps dump some transparent buttons or something over the NSOpenGLView area in Interface Builder?

The application is simple, i just need to know which area of a grid has been touched.


Solution:1

No need to add transparent buttons or overlays.

I'm assuming this is for the phone because you mentioned Cocoa Touch, but I'm not aware of NSOpenGLView for the phone. You need to look at the Apple example and create an EAGLView by overriding +layerClass in your subclass thus:

+ (Class)layerClass {      return [CAEAGLLayer class];  }  

Next, Make sure "User Interaction Enabled" is checked in IB.

Finally, implement the touch method calls:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;  - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;  

These will tell you where users touched the surface, and with how many fingers. You can do the rest from there. Try creating the template OpenGL project and looking at that. In addition, Jeff LaMarche has some good iPhone OpenGL tutorials on his blog: http://iphonedevelopment.blogspot.com/


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »