I have a simple program that does a calculation on a button press. The result is placed into a label using the following code:
//converts the float to 2 descimal places then converts it to a string
NSString *stringRectResult=[[NSString alloc]
initWithFormat:#"%1.2f",floatCalcResult];
//displays the string result in the label
resultLabel.text=stringRectResult;
It works perfectly, however, I added in code to hide the decimal keyboard when the user touches off the keyboard. That works, but when I added this code the button to update the label no longer worked. Can anyone help? The code to hide the keyboard is below. The app works when I comment it out but does not when it is active
In viewDidLoad:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(tap:)];
[self.view addGestureRecognizer:tapRecognizer];
Tap selector...
-(void)tap:(UIGestureRecognizer *)gr
{
[self.view endEditing:YES];
}
Thanks for any and all help.
The problem is that by intercepting all of the user's taps (in order to hide the keyboard), you're preventing any other user interface elements from being tapped. I would urge you to rethink your design; it's not usually necessary to have an explicit facility to hide the keyboard.
If you do want to keep this design, you can implement the gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: method in your class:
- (void) viewDidLoad
{
// ...
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(tap:)];
tapRecognizer.delegate = self;
[self.view addGestureRecognizer:tapRecognizer];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Two things to keep in mind:
You need to mark your view controller as conforming to the UIGestureRecognizerDelegate protocol.
If you later add a second gesture recognizer to the view, you'll need to add a check inside the second method to treat the second recognizer differently.
Related
I have a container view which has two UILongPressGestureRecognizers on it (well not only those two, but other gestures are acting ok). One is to recognize one finger, the other is for two. When i try to fire gesture for one finger, it will not fire it's target method until finger is lift off. The second one for two fingers is working as expected, firing selector action after short period of time. When i disable gesture for two finger press, the one finger press gets back to normal. How gestures are initialized, first is one finger gesture, initialized inside vc setup.
- (void) setupGestures
{
[self.contentView addGestureRecognizer:self.longGestureRecognizer];
}
- (UILongPressGestureRecognizer *)longGestureRecognizer
{
if (!_longGestureRecognizer) {
_longGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressAction:)];
[_longGestureRecognizer setMinimumPressDuration:1];
}
return _longGestureRecognizer;
}
The second one is initalized a bit later, but not from host VC, but inside view which is added to it
//Called in host
rullerView = [[RullerView alloc] initWithContent:self.contentView viewController:self];
and in init of view:
//This is called from within previous call initWithContent:ciewController:
- (void)setupGestures
{
UILongPressGestureRecognizer *rullerGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(rullerTapAction:)];
[rullerGestureRecognizer setNumberOfTouchesRequired:2];
//view here is a content view passed into init, basically the same one
//to which first recognizer is added, however selector action is contained
//in this view, and this view is target for this recognizer
[view addGestureRecognizer:rullerGestureRecognizer];
}
Could this be a problem?? Already tried
[gesture requireGestureRecognizerToFail:otherGesture];
and delegate method of view which recognizers are added to:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Hi I am developing small IOS application in which I want to display search bar and below it table view. In which I want to hide keyboard when user click outside. For that reason I am using tap recogniser but becoz of that my table view stops listening for row selection.
Here Is my code
//inside view did load
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
-(void)dismissKeyboard {
[_searchBar resignFirstResponder];
}
but because of this my row selection of table view get disable. that mean didSelectRowAtIndexPath never get called. Any one have solution for this. Need Help. Thank you .
Try adding this line of code this will solve your problem..
tap.cancelsTouchesInView = NO;
You should implement UIGestureRecognizerDelegate and add the following:
//inside view did load
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
tap.delegate = self;
[self.view addGestureRecognizer:tap];
// UIGestureRecognizerDelegate methods
#pragma mark UIGestureRecognizerDelegate methods
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isDescendantOfView:yourTableView]) {
// Don't let selections of auto-complete entries fire the
// gesture recognizer
return NO;
}
return YES;
}
There is a "Search Bar and Search Display Controller" in the Utilities panel that sounds like it would be perfect for you. This is a good tutorial that explains how to implement it. This way you won't have the keyboard issue anymore.
There is no need to use TapGestureRecognizer. Use SearchBarDisplayController
hope this will work for you
download a demo project
I am able to get the magnification of the body part where user has tapped. However, as mentioned the text (as "Wrist" in the image) should dynamically change depending on the body part and it should act as a button so that when tapped again it takes user to a different details screen via a function call. How can I achieve this functionality? Code snippets are welcome.
Maybe you can achieve the desired effect with a TapGestureRecognizer?
in viewDidLoad:
UITapGestureRecognizer *tapWrist = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tappedWrist)];
tapWrist.delegate = self;
[bodyView addGestureRecognizer:tapWrist];
Add the method:
- (void)tappedWrist
{
// code here
}
Don't forget to include:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
and add UIGestureRecognizerDelegate to your header file.
I recently got into trouble with a View Controller which has a UIWebView in it and a subview which I would like to add to the View Controller.
That's the View with the UIWebView:
http://cl.ly/image/03473l0e3a2L
My target is, to add a Share Menu which does work without problems:
http://cl.ly/image/3b273t2o3P00
But now I have the problem, that I set gesture recognizers for the social icons + labels (twitter,facebook,mail) - but these gesture recognizers don't do anything.
The ShareView is a UIView Subclass and I add the Gesture Recognizers this way:
UITapGestureRecognizer *fbTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(fbTapped:)];
fbTap.numberOfTapsRequired = 1;
fbTap.numberOfTouchesRequired = 1;
fbTap.delegate = self;
[fbImage addGestureRecognizer:fbTap];
[fbLabel addGestureRecognizer:fbTap];
UITapGestureRecognizer *twTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(twTapped:)];
twTap.numberOfTapsRequired = 1;
twTap.numberOfTouchesRequired = 1;
twTap.delegate = self;
[twImage addGestureRecognizer:twTap];
[twLabel addGestureRecognizer:twTap];
UITapGestureRecognizer *mailTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(mailTapped:)];
mailTap.numberOfTapsRequired = 1;
mailTap.numberOfTouchesRequired = 1;
mailTap.delegate = self;
[mailImage addGestureRecognizer:mailTap];
[mailLabel addGestureRecognizer:mailTap];
I think the Label and the UIImageView names do explain themselves. Every Label and ImageView has set userInteractionEnabled to YES. The ShareView is also enabled for Userinteractions and I did set UIGestureRecognizerDelegate to it.
The fbTapped,mailTapped and twTapped functions do send a Notification to the Main View (the view which has the webview and the ShareView in it).
But now when I click on the labels or imageviews, nothing happendes.
I did read on stackoverflow that the UIWebView in the MainView could interrupt the recognization? But I don't know how to solve this problem.
Would be really happy If you could help me or point me into the right direction to solve this problem.
I hope this piece of code will point you to the right direction:
1.Add a gesture recognizer delegate :
#interface myclass <UIGestureRecognizerDelegate>
{
//Whatever you are doing with gestures
}
2.Implement the delegate method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
//Do your stuff
return YES;
}
check if the control reaches this delegate
What I'm working with is I have a custom UIView that combines a UILabel with a UITextField as part of some user input. I have several of these throughout the view that is being displayed in the app. What would be good is to be able to have the user touch either the UILabel or the UITextField and then have the UITextField allow for input of the field.
Is there an easy way to do this?
Add a tap gesture recognizer to your label, and when tapped tell the corresponding textfield to become first responder (which brings up the keyboard).
// In your init or awakeFromNib:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelTapped:)];
self.label.userInteractionEnabled = YES;
[self.label addGestureRecognizer:tap];
...
- (void)labelTapped:(id)sender
{
[self.textField becomeFirstResponder];
}
Yes, there is and I actually tested it. You can add a tap gesture recognizer to your view (the one that contains the label and the text view) like so:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(viewTapped:)];
[self addGestureRecognizer:[tap autorelease]];
and then, inside viewTapped:
-(void)viewTapped:(id)sender {
[self.yourTextField becomeFirstResponder];
}
Hope this helps!
You can define UIGestureRecognizer delegate method and check that is the the required tap gesture area . If it is then return TRUE else return FALSE .
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// Disallow recognition of tap gestures in the segmented control.
if ((touch.view == yourButton)) {//change it to your condition
return NO;
}
return YES;
}