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.
}
Related
I have an overlay view (covering the entire screen), I can receive touches but it isn't passing them down the responder chain.
I add the subview like this:
UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow insertSubview:self.view aboveSubview:mainWindow];
It doesn't seem to matter if I use addSubview or insertSubview.
My touchesBegan looks like:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//do some stuff here..
[super touchesBegan:touches withEvent:event];
}
It doesn't seem to matter if I am using SUPER or various other combinations of self.nextResponder, etc..
In the end, I need to have an overlay view that covers the entire screen, I need to detect touches then pass them to the views/viewControllers below them.
Thanks!
You are addView to the mainWindow, your super view is the mainWindow, not the viewBlow it.
You need to add the view to VC's view, and if you want the full screen. set the status bar to hidden (or transparent. not tried) and then set the view's frame to (0, -20, weight, height+20).
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
My question might sound rather strange, but still I didn't find any reference and I really need help.
so my point is, I have a ViewController which has a collection of UIViews, to be specific a subclass of UIView with
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
overridden
from this event i want to load a specific data (according on the UIView touched) on the UIViewController's child UIWebView
Is there a way to address parent ViewControllers children from the UIView that receives the action
Or am I looking into a completely wrong direction.
Any hel will be greatly appreciated, thanks in advance.
You can use delegate for example:
#protocol someProtocol
- (void)recivedTouch:(UITouch *)touch fromUIView:(UIView *)uiView;
in you view:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
[self.delegate recivedTouch:touch fromUIView:self];
}
View controller implemed method from delegate:
- (void)recivedTouch:(UITouch *)touch fromUIView:(UIView *)uiView
{
if (uiView == self.someView)
{
// do some stuff with uiWebView
}
}
try to look at it differently. Your touchesBegan shouldn't do anything which is not connected with the view. Your view is just a UI component, it shouldn't control application flow (data loading). That's the job of the view controller.
Look how UIButton is implemented. The button does not do anything when clicked. The controller has to tell it "this is the method that should be called when the user clicks you"
There are two ways how to tell the view what it should do for a specific action - creating a delegate or passing a selector that should be called.
Consider also extending UIControl, which has most of the actions already implemented.
Alternatively use NSNotificationCenter: Send and receive messages through NSNotificationCenter in Objective-C?
Send a message from your view, hook the message from your controller. Notificationas can get a bit messy though, the delegate is probably a cleaner solution
I am trying to determine a Single Tab on a View inside of a UIScrollView. The Problem is that The UIScrollview catches all the gestures.
What I tried so far:
I override the following method in my UIScrollView:
-(BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
UITouch *touch = [touches anyObject];
if([touch tapCount]== 2) return YES;
return NO;
}
This works fine, I can now reach the UITapGestureRecognize on my UIView, unfortunately I can only detect double-taps because the [touch tapCount] == 1 is always beeing called (dragging or zooming in the UIScrollView). But actually the UIScrollview does not need the "Single-Tap-Function"
Is there a way to decide between a drag (Scroll or zoom) and a single Tap inside this method? I cant find it..
-(BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
Thanks in advance
Fabi
It sounds like you only want the tap recognizer to succeed if the touch doesn't scroll the scroll view. This is pretty easy because the scroll view uses a pan gesture recognizer for scrolling. On iOS 5, you can just do this:
[self.tapRecognizer requireGestureRecognizerToFail:self.scrollView.panGestureRecognizer];
If you want to support older versions of iOS, you have to do this:
for (UIGestureRecognizer *recognizer in self.scrollView.gestureRecognizers) {
if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]])
[self.tapRecognizer requireGestureRecognizerToFail:recognizer];
touchesShouldBegin: is a delegate method on UIView. It is not part of a UIGestureRecognizer.
You'll need to create a UITapGestureRecognizer, set the number of taps you want to trigger the recognizer and add it to your view.
Here is the documentation for UITapGestureRecognizer
I'm assuming you meant "single tap"--your scrollView belongs to the UIView tied to your controller. Assign the tap gesture recognizer to the that UIView. It gets first picks on all gestures and then passes down the ones it isn't interested in.
I want to personalize my UITableView changing background when user tap on a specific cell.
I've a dedicated ViewController for each cell and if I implement touchesBegan method in this viewController i can change my cell background without any problem. The problem is that the method "didSelectRowAtIndexPath" of the UITableView is no longer called. How can I call it manually? I am in another viewController and I have no access to that method. or how can propagate to the touch tableView?
Thanks a lot
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//do your operations
//send it to super class
[super touchesBegan:touch withEvent:event];
}
[super touchesBegan:touch withEvent:event];
Are you sending your action to super class? If not then add this line.