I have some textfield in which when i click it shows me keyboard , but when i click anywhere on the page the keyboard do not disappears . This is my code for resign of first responder. Whats wrong in my code.
Add this code:
-(void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapReceived:)];
[tapGestureRecognizer setDelegate:self];
[self.view addGestureRecognizer:tapGestureRecognizer];
}
-(void)tapReceived:(UITapGestureRecognizer *)tapGestureRecognizer
{
[textField resignFirstResponder];
}
Create iVar for UIGestureRecognizer
UIGestureRecognizer *tapper;
- (void)viewDidLoad
{
[super viewDidLoad];
tapper = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleTap:)];
tapper.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapper];
}
Dismiss what ever is currently editing:
- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
}
Try this code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[aTextField resignFirstResponder];
}
}
Or
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[self.view endEditing:YES];
}
}
- (void)viewDidLoad {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard {
[yourtextfield resignFirstResponder];
}
Add Tap Gesture on your Top View
you you havn't any subview which may hide your Root view
than only use self.view
if you are using ScrollView than add TapGesture on your ScrollView
-(void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDissmiss:)];
[self.view addGestureRecognizer: tap];
}
Handle Tap Gesture
-(void)tapDissmiss:(UITapGestureRecognizer *)tapGestureRecognizer
{
[self.view endEditing:YES];
}
Create one UITapGestureRecognizer as a class variable.
#interface ViewController() {
UITapGestureRecognizer *tapGesture;
}
- (void)viewDidLoad {
[super viewDidLoad]
tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(dismissKeyboard)];
}
Add gesture on textfield delegates textFieldDidBeginEditing, And remove it on textFieldDidEndEditing.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[self.view addGestureRecognizer:_tapGesture];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[self.view removeGestureRecognizer:_tapGesture];
}
Implement tap gesture selection
- (void)dismissKeyboard {
[self.view endEditing:YES];
}
Related
From my side I will try bellow code, but it is not working.
- (void) handleTouch:(UITapGestureRecognizer *) gesture
{
CGPoint touchPoint = [gesture locationInView:self.view];
NSArray *viewsAtPoint = [self viewsAtPoint:touchPoint];
for(TheifView * aView in viewsAtPoint)
{
[aView removeFromSuperview];
}
}
- (void) registerGesture
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTouch:)];
tapGesture.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapGesture];
}
Gesture target and selector can be removed by following code
[gesture removeTarget:self action:#selector(handleTouch:)];
Put tag to ThiefView...say 111. and then u can identify it and remove from self.view. If this is not the answer you expected then explain with clear description..
//For Removing Gestures from View..
for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers) {
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]) {
[self.view removeGestureRecognizer:recognizer];
}
}
//For Removing ThiefView from View
for (UIView *subview in self.view.subviews) {
if (subview.tag==111) {
[subview removeFromSuperview];
}
}
To remove it fully
- (void) handleTouch:(UITapGestureRecognizer *) gesture
{
[self.view removeGestureRecognizer:gesture];
}
To remove its target
- (void) handleTouch:(UITapGestureRecognizer *) gesture
{
[gesture removeTarget:self action:#selector(handleTouch:)];
}
To disable the gesture
- (void) handleTouch:(UITapGestureRecognizer *) gesture
{
gesture.enabled=NO;
}
I want to hide the keyboard by touching the view. Everybody recommends to use this method, saying there's no need to link or anything else, but is not working.
The problem is that my method is not called.Is there anything else that should be done?
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:YES];
}
I had trouble with this so use a method that loops through all views seeing if they are textviews and firstResponders. Not sure of the structure of a UITextView but you might need to check for that too although it may be covered.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView *txt in self.view.subviews){
if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
[txt resignFirstResponder];
}
}
}
The best approach is to create a "lock view" which is a UIView that takes over the whole screen once the textField becomesFirstResponder. Make sure it's on top of all views (well, besides the textview, of course).
- (void)loadLockView {
CGRect bounds = [UIScreen mainScreen].bounds;
_lockView = [[UIView alloc] initWithFrame:bounds];
_lockView.backgroundColor = [UIColor clearColor];
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(lockViewTapped:)];
[_lockView addGestureRecognizer:tgr];
[self.view addSubview:_lockView];
}
- (void)lockViewTapped:(UITapGestureRecognizer *)tgr {
[_lockView removeFromSuperView];
[_textField resignFirstResponder];
}
Use UITapGestureRecognizer For Dismiss keyboard.
Write this code on your viewdidload().
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];[self.view addGestureRecognizer:tap];
and in dismissKeyboard method put this code.
-(void)dismissKeyboard
{
[TextFieldName resignFirstResponder];
}
I'm wanting to invoke touchesEnded: method when the user TAPPED on a specific location on the screen but not when a user swipped on the screen. How can I achieve this?
From debugging, it seems that touchesEnded: is called when the user either tapped or swipped. I suppose I can add a local variable to track if the user had swipped like below but I think there is a more robust way of handling this:
BOOL trackSwipe = NO; // Local variable
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
trackSwipe = YES;
// do something
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if (trackSwipe == YES)
return;
else
{
CGPoint touchPoint = [[touches anyObject] locationInView:self];
// do additional work
}
}
I have also looked into adding UITapGestureRecognizer to invoke the tap selector but this approach doesn't allow me to find the touchPoint which is very important to have.
I appreciate any help on this. Many thanks.
You can try this using a UITapGestureRecognizer
ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIGestureRecognizerDelegate>
#end
ViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapMethod:)];
recognizer.numberOfTapsRequired = 1;
recognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[self.view addGestureRecognizer:recognizer];
}
- (void)tapMethod:(UITapGestureRecognizer *)recognizer
{
CGPoint touch = [recognizer locationInView:yourViewHere];
NSLog(#"X: %f Y: %f",touch.x,touch.y);
}
In your viewDidLoad do this:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
Selector method:
-(void)dismissKeyboard{
[self.view endEditing:YES];
}
Using this method you can determine tap gesture.
I want to takeout the keyboard when the user clicks on the background or any other item on the view.
I found out that the following code will take it off. But where should i add it.
[self.view endEditing:YES];
All my UI components are created programatically.
It's super simple, you only need to implement this code in your .m file:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
This will do.
Use below code
UITapGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
recognizer.numberOfTouchesRequired=1;
[self.view addGestureRecognizer:recognizer];
recognizer.delegate = self;
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
[userName resignFirstResponder];
[passWord resignFirstResponder];
return NO;
}
I would like to give a UIButton different features depending on if it is pressed and released once, or held down (ideally for 1.5 sec) and moved around the screen. I am using this code at the moment:
[button addTarget:self action:#selector(open:)
forControlEvents:UIControlEventTouchDown]
panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[button addGestureRecognizer:panRecognizer];
-(IBAction)open:(id)sender {}
-(void)move:(id)sender{}
move: works fine, but open: doesn't.
maybe you can use button's drag event, like
[button addTarget:self action:#selector(wasDragged:withEvent:)
forControlEvents:UIControlEventTouchDragInside];
You can look at this link: http://www.cocoanetics.com/2010/11/draggable-buttons-labels/
you can put a flag in move mehod, when enter touch up method, you check if the button has moved, to decide to trigger open related logic
#define BUTTON_DRAGGED_TAG -100
#define BUTTON_DEFAULT_TAG 0
- (void)onButtonTouchUpInside:(UIButton *)button withEvent:(UIEvent *)event
{
if (button.tag != BUTTON_DRAGGED_TAG)
{
//doOpen
}
button.tag = BUTTON_DEFAULT_TAG;
}
- (void)onButtonTouchUpOutside:(UIButton *)button withEvent:(UIEvent *)event
{
button.tag = BUTTON_DEFAULT_TAG;
}
- (void)onButtonTouchCancel:(UIButton *)button withEvent:(UIEvent *)event
{
button.tag = BUTTON_DEFAULT_TAG;
}
- (void)onButtonDraged:(UIButton *)button withEvent:(UIEvent *)event
{
button.tag = BUTTON_DRAGGED_TAG;
}
I did this, let me know if it works for you:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didRecognizeTapGesture:)];
tapGesture.cancelsTouchesInView = NO;
tapGesture.delegate = self;
[tapGesture requireGestureRecognizerToFail:self.scrollView.panGestureRecognizer];
[self.scrollView addGestureRecognizer:tapGesture];
self.tapGesture = tapGesture;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (touch.view == self.button)
{
return NO;
}
return YES;
}
- (void)didRecognizeTapGesture:(UITapGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateEnded)
{
NSLog(#"GESTURE ENDED");
}
}
- (void)didPressButton:(UIButton*)sender
{
NSLog(#"BUTTON TOUCH UP INSIDE");
}