UITableView not responding anymore after implement UITapGestureRecognizer - ios

In my view controller, I have a table view and a few custom UIView's. Within the view controller, a UITapGestureRecognizer is implemented for some interactive UI work. Now, the UITableView is not responding to UITapGestureRecognizer anymore. From debugging, it appears that it goes right into the UITapGestureRecognizer selector method, and the didSelectRowAtPath is never being called.
Thanks

First you need to Add Tap Gesture on your view.
And Remember <UIGestureRecognizerDelegate> to Add in .h file.
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTap1ImgView:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.delegate = self;
[self.ImgView addGestureRecognizer:doubleTap];
self.ImgView.userInteractionEnabled=YES;
- (void)doubleTap1ImgView:(UITapGestureRecognizer *)gesture
{
// Do What you want here
}

Related

Setting up Tap Gesture on UIView on an NSObject, even if delegate is set on View Controller doesn't work

in VideoPlayer.h:
#interface VideoPlayer : NSObject <UIGestureRecognizerDelegate>
#property (nonatomic, retain) UIView *playerView;
in VideoPlayer.m custom init method:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(userDidTapScreen:)];
tgr.delegate = self; // tried with and without
tgr.cancelsTouchesInView = NO; // tried with and without
[self.playerView addGestureRecognizer:tgr];
....
- (void)userDidTapScreen:(UITapGestureRecognizer *)tap {
NSLog(#"user tapped");
}
Because this object is part of an SDK, we don't have a view controller. But even if I set the delegate in the View Controller of a project using the SDK, I still have no luck. If I had another view as a subView to self.playerView, the tap gesture works on that view, even if I'm still adding gesture to self.playerView. I have removed all subviews to make sure nothing is blocking and when I do that, still no luck. I know that typically view controllers handle this and if I put all the tap gesture code in the view controllers, it works, but if I only set the view controller as the delegate and that's the only gesture code on the view controller, it doesn't work.
When added to a project's view controller:
VideoPlayer *player = [[VideoPlayer alloc] initWithFrame:CGRectMake(10, 100, 320, 200)];
[self.view addSubview:player.playerView];
UITapGestureRecognizer * recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
recognizer.delegate = self;
[player.playerView addGestureRecognizer:recognizer];
This isn't ideal for our SDK. I think we could tell anyone using it to set delegate in their view controller, but that doesn't even work when that's the only line in the view controller.
Any help would be greatly appreciated. I've done a lot of tap gestures both in code and through IB and never this problem.

Close keyboard when view is pressed - Obj C

I have a function that populates a few fields into the self.view.window view. Now if I run:
/* HIDE KEYBOARD WHEN CLICKED AWAY */
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
gestureRecognizer.cancelsTouchesInView = NO; //so that action such as clear text field button can be pressed
[self.view addGestureRecognizer:gestureRecognizer];
within my viewDidLoad it works great. But lets say I click a cell -> calls function -> displays using: [self.view.window addSubview:self.DatePicker];
MygestureRecognizer will not longer work. I assume that is because of sending it to the window. My question is, how can I get that to work?
UPDATE:
The reason I am adding to the window is because I need to give it a fixed like property and that did the trick for me. For instance in CSS you are able to do position:fixed;
I figured out a cheap workaround but if you have a better idea please let me know.
Because I have cells that are populated in a uitableview I am listening for a specific cell that is clicked. So what I did is just create a gesture container and add that to the view.window on that specific cell that I listened for and that did the charm.
[self.view.window addGestureRecognizer:self.gestureRecognizer];
ADD TAP GESTURE ON VIEW
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapBehind:)];
recognizer.delegate = self;
[recognizer setNumberOfTapsRequired:1];
recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view
[self.view.window addGestureRecognizer:recognizer];
handleTapBehind Method :
- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
[self.view.window removeGestureRecognizer:sender];
// Dismiss Your Keyboard HERE
}

Add swipe gesture to UITableView

How would I make it so that one gesture anywhere on a UITableView would do something?
Not just within one cell, but anywhere on the screen (a horizontal swipe)?
- (void)viewDidLoad
{
[super viewDidLoad];
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
//There is a direction property on UISwipeGestureRecognizer. You can set that to both right and left swipes
recognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
[tableView addGestureRecognizer:recognizer];
[recognizer release];
}
Finally
Just return UITableViewCellEditingStyleNone in your tableView:editingStyleForRowAtIndexPath: method.
If you want to make it dead simple using the interface builder you can apply the gesture on a ViewController that contains only the TableView and show that ViewController inside a container.

How to navigate to next UIViewController using UITextView in an UINavigationBar

I'm trying to use an UITextView as a button, and after clicking on the UITextView it should pop to to another UIViewController (not the root one, next one).
Using an UIButton works fine (by dragging to next UIViewController and choosing "Push" as the segue type, without doing any coding). But, doing the same thing with a UITextView does not work!
So, my question is how to navigate to the next view controller using an UITextField?
Add Tap Gesture to UITextView
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(mySelector)];
tapRecognizer.numberOfTapsRequired = 1;
[_textView addGestureRecognizer:tapRecognizer];
Push or Present UIVieWController in UITapGestureRecognizer action method.
-(void)mySelector
{
// Navigate to next view controller
}
Try adding a UITapGestureRecognizer to the text view and using that callback to trigger a segue.
I don't think you can do this without code.
Set the userInteractionEnabled attribute?
textView.userInteractionEnabled = YES;
You should be able to use the textView like a button...
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(myTapped:)];
tapRecognizer.numberOfTapsRequired = 1;
[txtView addGestureRecognizer:tapRecognizer];
-(void)myTapped::(UIGestureRecognizer *)gesture
{
// Do what you want here
}
try this code

UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer

I have a simple UIViewController derived controller which has UITextFields.
Also using IB, I placed a UIToolbar and two UIBarButtonItems. I Ctrl-Drag to add actions to the buttons.
- (IBAction)cancel:(id)sender { ... }
- (IBAction)save:(id)sender { ... }
If I run the code, the actions get called.
The problem: I wanted to implement the tap on background to resignFirstResponder paradigm, so I added a UITapGestureRecognizer on the root view:
- (void)viewDidLoad {
...
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
When I have the gesture recognizer, the actions of the UIBarButtonItems are not called
How can I have it both ways?
Thanks for any explanations that could help me implement this.
I resolved the problem by adding another view and setting the gesture recognizer on that view instead of the root view.
But I still would like to know the explanation to why the gesture recognizer would "eat" the action of the UIBarButtonItem.

Resources