views:

63

answers:

1

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.

+1  A: 

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/

Paul Franceus
NSOpenGLView only exists on the Mac. The questioner mistagged his question; I've removed the cocoa-touch tag.
Peter Hosey