UIScrollViewKeyboardDismissModeOnDrag when nothing to scroll doesn't work - ios

I'm trying to hide the keyboard with swipe gesture, in iOS 7 i know there is UIScrollViewKeyboardDismissModeOnDrag but this seems to work only when there is something to scroll, instead when the row result of the search are under the UIKeyboard but there are not enough row to scroll the view this UIScrollViewKeyboardDismissModeOnDrag is not working, because there is nothing to scroll, how i can dismiss the keyboard with a swipe gesture also when there is nothing to scroll?

The simplest and most elegant code wise, and the nicest feel in use, is to always set
self.tableView.alwaysBounceVertical = YES;
when using UIScrollViewKeyboardDismissModeOnDrag.
That way you can always drag vertically against a bounce, so the issue doesn't exist.

This work for me:
UISwipeGestureRecognizer * tapGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(hideKeyBoard)];
[self.view addGestureRecognizer:tapGesture];
And the method:
-(void)hideKeyBoard {
[textViewOrTextField resignFirstResponder];
}

Hope that will help (at least it works for me).
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[myTextField resignFirstResponder];
}

Related

UITapgesture with UIScrollview not stop scrolling?

i am facing problem, i don't know how to achieve this give me any solution.
UIView
----UIScrollview
------Imageview (minimum 10)
Every Imageview added into scrollview, and every ImageView contains UITapgesture recongizer.Tapgesture used to take the image and show into Nextviewcontroller as enlarged.
Here i am facing problem, when i scrolling the UIScrollview i want to stop scrolling right over there by touching inside or somewhere of scrollview.If i touch anywhere, its going next page.
In the page, if the UIScrollview is scrolling (without the user currently touching the screen) and then the user taps the screen, the screen should stop but no Imageview should be enlarged. Currently the screen stops, but the clicked picture is being enlarged.
Code so far...
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapAction:)];
singleTap.numberOfTapsRequired = 1;
singleTap.delegate = self;
singleTap.cancelsTouchesInView = NO;
hypeScroll.canCancelContentTouches = NO;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
//return YES;
// if (gestureRecognizer.view.superview.tag == hypeScroll.tag) {
// return NO;
// }
return YES;
}
Give me solution, i have stuck in long time,
thanks in advance
I think UICollectionView will be more helpful for you in achieving this functionality rather than relying on UIScrollView. Check out UICollectionView.

How to detect a tap outside a UITextField (to dismiss key board)

My iPhone App has a numerical text-field (and some other controls). A num pad is shown when the user taps the text field. I like to dismiss this num pad (by [self.textField resignFirstResponder]) as soon as the
user taps on the view's background (easy to detect) but also when the user taps on any other control!
How can I detect that the user taps outside the text field (but not necessarily on the background)?
I always do this in viewDidLoad:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
and then implement:
- (void)dismissKeyboard
{
[self.view endEditing:YES];
}
Works like a charm :)
Just use:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
Extending on #reecon's answer about touchesBegan. I wouldn't recommend to use it as you'd end up with a jumping keyboard up and down when the user taps repeatedly and believe me, it looks totally weird (at least it was on iOS 7.1.1 in one of my projects).
I'd go for the good old full screen UITapGestureRecognizer. You could also use a full screen UISwipeGestureRecognizer with a down direction - it would look and feel even cooler, as if the user is sliding down a keyboard.
Both gestures are added through the IB and require no code to set up.
Tapping on "other controls" is a totally different story though and you'd have to do it all by hand - no gestures can help you.

Hide UITableView on touches outside the tableview

I have a small UITableView that is hidden when the view is loaded. When i click on "SHOW" UIButton, the UITableView is made visible by myTableView.hidden=NO;
I want to hide UITableView when a user touches outside its frame. Thanks for any help!
Best Approach
Simple.Before show up the UITable View add one more grayed out/Transparent view then add tap gesture recogniser on it to hide it . That's it.
Show Overlay View first - alpha will be 0.5f and background color should be clear color.
show the table view.
NOTE: over lay view should have tap recogniser which will hide the overlay and table view
in View did load
UITapGestureRecognizer *tapRecog = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(overlayDidTap:)];
[myOverLayView addGestureRecognizer:tapRecog];
- (void)overlayDidTap:(UITapGestureRecognizer *)gesture
{
//hide both overlay and table view here
}
Bad Approach
We should not add tap recogniser on main view itself. Because it may have lots of
controls inside of it. so when user tap on it. it will perform its operation. So to avoid
it we can simulate the same behaviour by above approach
You can get touch position by this:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[self.view addGestureRecognizer:singleTap];
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint=[gesture locationInView:self.View];
}
Then just check if the point is in tableview frame. If not then hide the tableview. Hope this help. :)
Subclass UITableView, override pointInside:withEvent:. It is templated for this reason.
Something like:
-(BOOL)pointInside:(CGPoint) point withEvent:(UIEvent*) event
{
BOOL pointIsInsideTableView = [super pointInside:point withEvent:event];
BOOL pointIsOutsideTableView = // Some test
return pointIsInsideTableView || pointIsOutsideTableView;
}
So you can catch the touch in table view implementation, where it belongs logically in this case.

iOS UITaprecognizer not responding

I am trying to add a tap recognizer to a UILabel programmatically. It is not working... I have searched and tried a millions things of stackoverflow, but I can't get it to work...
Here is my current code:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(more:)];
[self.moreLabel setUserInteractionEnabled:YES];
[self.moreLabel addGestureRecognizer:tap];
- (void)more:(UITapGestureRecognizer *)sender {
NSLog(#"HIT?");
}
It will work if I add it to the top view but I don't want that ;) Any help is appreciated.
Solved using #Savitha comment:
Summary of answer: "The view I was trying to touch wasn't under another view so either using bringToFront or setting it correctly in IB was the answere."
Bring moreLabel to front.
[self.view bringViewToFront:self.moreLabel];

Strikethrough Text with Animation iOS

What is the best way to display a strikethrough animation? When a user swipes their finger across a UITableViewCell, I would like to animate a thin line across cell.textlabel.text
The two ways I've thought of so far would be using Animation or somehow displaying a custom image and revealing it slowly from left to right? Does anybody have any advice on this?
I already have the swipe gestures working, I now just need to know how to make the animation happen:
Add Gesture Recognizer:
//Add a left swipe gesture recognizer
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSwipeLeft:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.tableView addGestureRecognizer:recognizer];
//Add a right swipe gesture recognizer
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSwipeRight:)];
recognizer.delegate = self;
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.tableView addGestureRecognizer:recognizer];
Delegate Methods for Gestures:
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
NSLog(#"uncompleted");
}
// Cross Item off of the list
- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
NSLog(#"completed");
}
Based on what you have so far the following should work:
when handleSwipeLeft (or right) fires, place a new UIView with a black background color over the textfield at the textfield's x point and around halfway to the y point with a width of 0 and height of 1
then, call [UIView animationWithDuration....] changing the UIView's width property to be roughly the width of the textfield.
This should be close to what you want with some tweaking. I don't think it will be possible to animate the strikethrough from using the properties of the font alone but this technique should simulate it just fine.
Good job on getting half way there.

Resources