UITapgesture with UIScrollview not stop scrolling? - ios

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.

Related

UITapGesture for Button are working. But, long press gesture for the same are not working

I have a table view with X number of rows.
One of the row has collection view inside it.
Collection view has Y number of cells.
Every cell has a button.
Now, the problem is; If I apply a tap gesture for button inside the collection view cell it works.
But, when I apply a long press gesture to button it is not working at all.
Following is the code that I am trying.
-(void)setupGestureForAllButton{
for (UIButton *button in self.keyboardButtons) {
UILongPressGestureRecognizer *gr = [[UILongPressGestureRecognizer alloc] init];
[gr addTarget:self action:#selector(userLongPressed:)];
[button addGestureRecognizer:gr];
}
}
- (void)userLongPressed:(id)sender {
NSLog(#"user long pressed");
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Please find the pictorial representation of the problem
UPDATE 1 :
setting gr.delegate = self; did the trick.
I am still not sure; then, why it was working for tap gesture in the first place.

UICollectionView within UIScrollView: Long Press

I have added a UILongPressGestureRecognizer to my UICollectionView that is within a subclass of UIScrollView. (The UIScrollView is paged so there are 3 horizontally stacked UIViewControllers).
My code to add the UILongPressGestureRecognizer:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPress:)];
longPress.delegate = self;
longPress.minimumPressDuration = 0.5;
longPress.delaysTouchesBegan = YES;
[self.collectionView addGestureRecognizer:longPress];
And an NSLog in my handleLongPress: method. Currently I hold down on a UICollectionViewCell, it highlights, but the long press is not activated. I believe my UIScrollView subclass is consuming the long press and not passing along to the UICollectionView. When I lift my finger, the didSelectItemAtIndexPath: method is called.
In my UIScrollView subclass, the only customization I have is the following:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer {
// This line enables the swipe to delete in the Messaging VC.
return ([otherGestureRecognizer.view.superview isKindOfClass:[UITableView class]]);
}
This was done to enable cell swiping in my UITableView, which is one of the pages of my UIScrollView. The swiping works no problem, and I have tried a number of similar checks for UICollectionView and UICollectionViewCell here but have not gotten the long press to register yet. Any advice appreciated.
Edit: I have added the long press on another UICollectionView and it is functional, but the cell never shows highlighted/selected status. I guess that is a clue as to why I can't get this long press gesture to fire.
My issue was that I was adding the gesture recognizer in the -init method. That did not work. Simply moving the code to -viewDidLoad fixed the problem.

How to add a UILongPressGestureRecognizer and UITapGestureRecognizer to the same control simultaneously?

It is similar to this question:
iPhone iOS how to add a UILongPressGestureRecognizer and UITapGestureRecognizer to the same control and prevent conflict?
but my problem is more complex.
I want to implement the same behavior you see in iOS 8 on iPad. I mean page grid in Safari.
The problem: one view should respond to both long press and tap gesture recognizers. The following things should work:
1)close button accepts clicks
2)when the tap begins the selected view should perform scale animation
3)on long press the selected view becomes draggable
If I don't use (requireGestureRecognizerToFail:) then tap gesture doesn't work. If I use this method then everything works but the long press events take place with huge delays.
How to solve this issue.
You need to use the requireGestureRecognizerToFail method.
//Single tap
UITapGestureRecognizer *tapDouble = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleTapGestureForSearch:)];
tapDouble.numberOfTapsRequired = 1;
tapDouble.delegate = self;
[self addGestureRecognizer:tapDouble];
//long press
UILongPressGestureRecognizer *longPressGestureRecognizer=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(handleLongPressRecognizer:)];
longPressGestureRecognizer.numberOfTouchesRequired=1;
longPressGestureRecognizer.minimumPressDuration = 0.5f;
[longPressGestureRecognizer requireGestureRecognizerToFail:tapDouble];
longPressGestureRecognizer.delegate = self;
[self addGestureRecognizer:longPressGestureRecognizer];
This means Long press gesture wait for the single Tap.
You can add time to the long press gesture.
UILongPressGestureRecognizer *longPressGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(ontappLongPressGesture:)];
longPressGesture.minimumPressDuration=0.6;
longPressGesture.delegate=self;
[cell.view addGestureRecognizer:longPressGesture];
UITapGestureRecognizer *gesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(cellSelected:)];
//[gesture requireGestureRecognizerToFail:longPressGesture];
gesture.delegate=self;
[cell.view addGestureRecognizer:gesture];
also you need to set this delegate to work both gesture together
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}

UIScrollViewKeyboardDismissModeOnDrag when nothing to scroll doesn't work

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];
}

GestureRecognizer Interferes w/ MapKit Popup

I have a simple MapKit app working fine in iOS. It has annotation and when the user clicks on them, the little gray default popup is displayed with the title / subtitle. I even added a UIButton view into it.
So the problem is, I have a search bar above my map. I wanted to resignFirstResponder from the search box whenever the user clicks on the MapView, so I added a simple tap gesture responder. Worked great except now the little gray detail popups no longer show up (only the annotation pins)! I can still tap, zoom, move around etc. Just no popups.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
tap.cancelsTouchesInView = NO;
tap.delaysTouchesBegan = NO;
tap.delaysTouchesEnded = NO;
[mapView addGestureRecognizer:tap];
-(IBAction)tapped:(UITapGestureRecognizer *)geture {
[searchBar resignFirstResponder];
}
Is it possible to have the best of both worlds?
I used a delegate method similar to the following to arbitrate between touches that should go to my custom view's pan gesture recognizer and touches that should go to the scroll view that contained my custom view. Something like it might work for you.
// the following UIGestureRecognizerDelegate method returns YES by default.
// we modify it so that the tap gesture recognizer only returns YES if
// the search bar is first responder; otherwise it returns NO.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ((gestureRecognizer == self.tapGestureRecognizer) &&
(gestureRecognizer.view == self.mapView) &&
[searchBar isFirstResponder])
{
return YES; // return YES so that the tapGestureRecognizer can deal with the tap and resign first responder
}
else
{
return NO; // return NO so that the touch is sent up the responder chain for the map view to deal with it
}
}

Resources