How to detect UITapGestureRecognizer 's clicked view? - ios

I am using :
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard:)];
[self.view addGestureRecognizer:tap];
in order to close keyboard when clicked anywhere else from UITextField.
However in my view, I have UITableView , and I have to detect when click on the rows of UITableView.
Because of UITapGestureRecognizer my didSelectRowAtIndexPath function is not called. Is there any way to detect whether the clicked object is UITableViewCell?

You can get thouch event of Gesture from
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
you detect touch of gesture so do logically like bellow:
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UITextFiled class]])
{
return FALSE;
}
else
{
// here is remove keyBoard code
return TRUE;
}
}

Implement this delegate method gestureRecognizerShouldBegin:, check and cancel your gesture callback which happen on tableview cell(row) and trigger tableview's delegate method.
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer.view isKindOfClass:[UITableViewCell class]])
{
return NO;
}
return YES
}

Related

How to Disable Tap Gesture for Scroll view's sub view?

I have created one scrollview for registration. I have also use tableview for drop-down(Male/Female) and tap Gesture on scrollview for hide all input views because touchesBegan method dose not call. whenever i click on table view cell,it will not call didselect method but consider as tap on scrollview.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
tap.numberOfTapsRequired=1;
[_scrollview_out addGestureRecognizer:tap];
Method:-
-(void)dismissKeyboard
{
[_txt_name resignFirstResponder];
[_txt_mname resignFirstResponder];
[_txt_surname resignFirstResponder];
[_txt_gender resignFirstResponder];
[_txt_dob resignFirstResponder];
_tbl_view_gender.hidden=true;
_txt_dob.hidden=false;
_lbl_dob.hidden=false;
}
Table View didSelect Method:-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[_scrollview_out endEditing:YES];
str2=[gender_arr objectAtIndex:indexPath.row];
_txt_gender.text=str2;
_tbl_view_gender.hidden=true;
_txt_dob.hidden=false;
_lbl_dob.hidden=false;
}
for me there are two options:
1: add the tableview in scrollView's superView
2: subclass UIScrollView, overwrite method:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
//if the touch point in the tableview.
//return NO;
//else
//return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isDescendantOfView:Your Tableview]]) {
// Don't let selections of auto-complete entries fire the
// gesture recognizer
return NO;
}
return YES;
}

exception happened when setting UIPanGestureRecognizer delegate

I need to detect the length of horizontal scroll and do something
UIPanGestureRecognizer *panGestureRecognizer;
panGestureRecognizer=[[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(doSomething:)];
at the same time allowing user to scroll vertically as normal.
So I set UIGestureRecognizerDelegate, but exception happens.
MyGestureDelegate *deleg = [[MyGestureDelegate alloc] init];
[panGestureRecognizer setDelegate:deleg];
[self.scrollView addGestureRecognizer:panGestureRecognizer];
Here's my delegate.m
#interface MyGestureDelegate : NSObject <UIGestureRecognizerDelegate>
#end
#implementation MyGestureDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}
what can I do to make it right?

How to use the system pop gesture in a UIScrollView?

I have a UIScrollView in a UINavigationController, but I find I can't use the system's pop gesture when I touch on the scrollView,how should I fix the conflict?
Fixed,the custom left navigation item causes this issue,I add the code
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
but still not work in some situation, it should be modify like
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}

Objective C - UIPanGestureRecognizer selector not invoked with UISlider

I am trying to using UIPanGestureRecognizer and UITapGestureRecognizer on UISlider. tap is working fine i am facing problem with UIPanGestureRecognizer. I need to call handleTransactionPan method on changing value of UISlider but it is never being called because of its delegate method.
How can i invoke the selector while keeping the delegate methods to fire? Here is the code for what i have done so far.
- (void)viewDidLoad
{
[super viewDidLoad];
pr=[[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleTransactionPan:)];
pr.delegate=self;
[slider addGestureRecognizer:pr];
tr=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
tr.delegate=self;
[slider addGestureRecognizer:tr];
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if(gestureRecognizer==pr)
{
if ([touch.view isKindOfClass:[UISlider class]]) {
return NO;
}
}
return YES;
}

IOS: hide keyboard on touch UITableView

I am new in iOS development. I want to hide the keyboard when tapping outside of a UITextView.
My TextView is in a cell from an UITableView. The problem is that I have a Toolbar at the top and my buttons doesn't react anymore. I implemented the method "shouldReceiveTouch" but my test is not correct i think. Any ideas? Thank you and sorry for my bad english..
In my ViewDidLoad:
tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
tap.delegate = self;
[self.view addGestureRecognizer:tap];
note: tap is an UITapGestureRecognizer property.
Implemented methods:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UIBarButtonItem class]]) {
return NO;
}
return YES;
}
-(void)dismissKeyboard {
[tview resignFirstResponder];
}
UIBarButtonItem is not a subclass of UIView, hence the shouldReceiveTouch still return YES.
Try to exclude the whole UIToolbar or just add the tap gesture recognizer in the UITableViewCell when you initialize the cell in cellForRowAtIndexPath.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UIToolbar class]]) {
return NO;
}
return YES;
}
You should add your gesture to table view.
tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
tap.delegate = self;
[tblView addGestureRecognizer:tap];
use didScroll method of UIScrollView delegate to resign keyboard.TableView is also subclass of UIScrollView so it should work.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[tview resignFirstResponder];
}
or use this one
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[tview resignFirstResponder];
}
If you still want to use gesture then add gesture to UIView or self.view or superView of tableView
instead of adding it to tableView
Try following code :----
Keep you code as it is and add this method
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[tview resignFirstResponder];
}
in viewDidLoad set self.view.userInteractionEnabled = yes;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] == tview) {
[tview resignFirstResponder];
}
}

Resources