Touch Events on UIView on UITableView - ios

I'm trying to catch the touchesBegan and the touchesMoved events on an UIView.
I have a UITableView; each cell contains a UIView with several controls. One of these controls is a subclass of a UIView with code in the touchesBegan and touchesMoved events.
If this control is not on the tableviewcell, then it works fine. But in this situation, all touches have effect to the tableview, but not to the UIView.
How to ignore the TouchEvents and redirect them to the UIView?

Try setting delaysContentTouches property of the tableview(scrollview) to YES.

Try this one..
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isDescendantOfView:yourview])
{
return NO;
}
return YES;
}

Related

Are subviews automatically added as listeners to their superview's UIGestureRecognizers?

I have a swipe gesture attached to a UIView that doesn't seem to be registering when the swipe is on top of it's subviews.
One of solution is to check is gesture point inside your subview or not,
there is a useful C function:
/* Return true if `point' is contained in `rect', false otherwise. */
bool CGRectContainsPoint(CGRect rect, CGPoint point)
that you can use like this:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return CGRectContainsPoint(subview.frame, [touch locationInView:self.view]);;
}
The other solutions should work, but a potentially easier one is to set subview.userInteractionEnabled = false in the subview if it doesn't have its own event handlers.
Let's say A is the root UIView which you want to receive swipes, and B is a subview of A that you don't really want to receive swipes.
if you do not want to receive any gestures on B, you can userInteractionEnabled = false on it
if you still want to receive some gestures on B (but not a swipe)
you must subclass B so that you can implement this method, and implement this method in B
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([gestureRecognizer isKindOfClass:UISwipeGestureRecognizer]) {
return NO;
}
return YES;
}
You can also see how you may get a wide range of functionality from implementing gestureRecognizer:shouldReceiveTouch:

Adding a UIGestureRecognizer taking priority over all other interactions

When I tap on a UIButton, a UIView MyView appear from the bottom a cover a third of the screen. I would like that when I tap somewhere outside this view, it disappears.
I thought about adding another transparent UIView right under MyView and add a tab gesture on it with the dismiss function but I'm sure there is something cleaner than this.
So I thought about adding the tap gesture MyTapGesture to dismiss MyView on self.view of the UIViewController. The problem is that outside this view, I have other UIControls and gestures that capture also any touch at the same time than MyTapGesture.
How can I make MyTapGesture the priority gesture outside MyView and ignore all other gesture, taps, etc...?
You may have to use the gesture delegate methods to handle two tapGestureRecognizer activate the one you need depending on scenario
#pragma mark - UIGestureRecognizerDelegate methods
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if ([tapGestureRecognizer1 isEqual:gestureRecognizer]) {
return [tapGestureRecognizer2 isEqual:otherGestureRecognizer];
}
if ([tapGestureRecognizer2 isEqual:gestureRecognizer]) {
return [tapGestureRecognizer1 isEqual:otherGestureRecognizer];
}
return NO;
}

gesture recognizer is not working on subview

I have a UIView which has a UILabel view as a subview. Now I have added gesture for self (which is UIView). UIView recognizes the touch but subview(UILabel) doesn't.
There are similar questions in stackoverflow which tells the below property to set YES. I tried but it doesn't solve.
I have also set label.userInteractionEnabled = YES.
Try this
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UILabel class]])
{
return YES;
}
return YES;
}
You may need to set the delegate of gesture recognizer as self

Disable Touches on a view

Hi I have a UIView who's alpha is 0.7 and below it are some UITextFields. I don't want it to call touches events while keeping touches events. I tried using
[lightBoxView setUserInteractionEnabled:NO];
But now the UITextFields can become active or first responder. How can I disable it from calling touch events as well as not passing touches to others?
You also need to set the userInteractionEnabled = NO for all the subviews as well.
Try this,
[[lightBoxView subviews] makeObjectsPerformSelector:#selector(setUserInteractionEnabled:)
withObject:[NSNumber numberWithBool:NO]];
This will call setUserInteractionEnabled: on all direct subviews of lightBoxView and set it to NO. For a more complex subview hierarchy you will have to recursively loop through all the child views and disable the user interaction on each one. For this you can write a recursive method in a UIView category. For more details about this category method take a look at this answer.
Hope that helps!
You can remove those control from tough gesture delegate method.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UITextFiled class]])
{
return NO;
}
else
{
return YES;
}
}

How to unblock pan and pinch events in a UIScrollViewDelegate?

I have a UIView which has a UIScrollView subview and is a UIScrollViewDelegate. Since I've implemented viewForZoomingInScrollView I stopped getting pan and pinch events for the UIView.
How can I get these events back?
Try adding a delegate to your gesture recognizer that implements
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
try:
[yourviewname setUserInteractionEnabled:YES];
Edit: I just re-read your question. Are you saying that you can't pan and/or pinch? Or are you saying that you aren't being notified when you pan and/or pinch?

Resources