Detecting touches on a NSOpenGLView - objective-c

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.

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/

Related

Interactive touch display

I'm looking to intercept all touch events on a parent UIView and display where the user touches but still bubble those touch events to children views i.e.. UIButtons, UITableViewCell, etc. so those children interact via their default manner.
I have no issues displaying touch events interactively on UIView using basic delegate methods:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
}
My issue at this point is bubbling these events to next responder when UIView contains other interactive children views such as UIButtons. touchesBegan: is never fired on parent view when tapping a button so I'm not able to display something "pretty" to user when taps buttons.
Is there a better way other than creating category for UIButton, UITableViewCell? Thanks
Here is an example of what I'm looking to do anytime user touches the screen... (notice the white circle when user touches buttons):
https://m1.behance.net/rendition/modules/154366385/disp/9532a948c71e28ffda2219600c05ffd9.gif
You can do it by creating a subclass of UIView.
The view should be on the top of the view hierarchy and its size should be full of the screen.
All touch events will be sent to this view. If you return NO in the pointInside:widthEvent: method, the touch event will be sent to the lower objects.
#implementation MyView
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
// show bubble here
return NO; // send touch events to lower layer objects.
}

How do I forward touch events to a view behind a uicollectionview?

I have a uicollectionviewcontroller which I use to create a small toolbar at the bottom of the screen. I add the view of the uicollectionviewcontroller to another viewcontroller's view. My problem is that it won't forward touch events to a view underneath it. I did move it to the bottom and that worked for a while but now I need to use 2 uicollection views.
How am I supposed to forward touch events to views beneath a uicollectionviewcontroller's view?
In case the question is still relevant, the following can be used to achieve touch forwarding.
Subclass UICollectionView
Overwrite - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesEnded:touches withEvent:event];
[self.yourCustomDelegate forwardTouches:touches withEvent:(UIEvent *)event];
}
That is how UICollectionView can keep its ability to respond to user interaction, and the same time forward received touches.
As i think you can add your toolbar as footer of UICollectionView and it will work
have a look into this link How to Add Header and Footer View in UICollectionView

Freeform Container UIView

I am trying to understand if something like this is possible and if yes how can i go about achieving this.
i want to create a UIView that it is a container for other views that respond based on the below picture. i know that a UIView is a container of other UIViews. what i want to be able to do is to be able to touch on a UIImageView and when i am dragging it in the main View, if it gets out of bounds on one side, to be shown on exactly the opposite direction like shown on the image. i hope i managed to explain the question correctly.
Click Here for the Description
You can achieve that very easily by implementing the container UIView's touch methods:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
In touchesMoved you can check for your UIImageView's origin. If it crosses the boundaries (x, y less than 0 or bigger than the container's dimensions) then simply reposition its frame by your desired offset.

UIGestureRecognizer or touchesBegan not responding

I have a normal app. Yes this will not work anywhere in it. If I place this in a ViewController, it will not work, if I place this in a UIView subclass, likewise, it refuses to work. What am I doing wrong?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Please Work");
}
Make sure the view has userInteractionEnabled=YES. If a parent view has userInteractionEnabled=NO, the touches will be lost too. If that doesn't help, post code.

touchesEnded method doesn't get called?

I am developing an application in which I use
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
}
-(void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
}
Now my problem in iphone 3 after touch move it is calling handleSingleTap function and touchesEnded didn't get called. This happen only on some lower part of the screen when I move there touch end didn't get called.If I move long then touchesEnded get called.
But this thing is working properly in IPhone4 and IPad.
In IPhone4 and IPad after touchsesMoved it is calling touchesEnded.
You're not the only one who has encountered this problem. I found a similar thread on Apple Support forum list : https://discussions.apple.com/thread/1507669?start=0&tstart=0
This thread dates from 2008 which coincides with the iPhone 3, so I guess there might be a chance this is an unfixed bug on iOS running on iPhone 3.
Otherwise if it's not the case, may be it's due to the fact that you are using a UIScrollView (whose controller is implementing the touch methods you listed above). By default, it has a method called :
delaysContentTouches
That is set to YES. May be you should change it to no and say how it behaves.
If you are not using a UIScrollView, may be you are using two view and the second view containing the "dead area" is not connected to your controller ?