WebView and UISwipeGestureRecognizer - ios

I am trying to have a UISwipeGestureRecognizer that is attached to a webview. This works, but I need to also be able to scroll up and down the web view.
Here is my code:
UISwipeGestureRecognizer *upSwipeGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(tapAction)];
upSwipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
upSwipeGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:upSwipeGesture];
[webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:upSwipeGesture];
If I comment out the last line, then the webview scrolls but the gesture is not recognized. If I do not comment out the last line, then the gesture is recognized but the webview does not scroll.
I would like to be able to scroll and have the gesture be recognized. Any ideas? Thanks!

UIWebView has its own private views, which also has gesture recognizers attached. Hence, precedence rules keep any gesture recognizers added to a UIWebView from working properly.
One option is to implement the UIGestureRecognizerDelegate protocol and implement the method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer. Return YES from this method for other tap gestures.
This way you'll get your tap handler called, and the web view will still get its called.
Try this,
UISwipeGestureRecognizer *upSwipeGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(tapAction)];
upSwipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
upSwipeGesture.cancelsTouchesInView = NO;
upSwipeGesture.delegate=self;
[self.view addGestureRecognizer:upSwipeGesture];
ind implement this delegate method,
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}

Related

Gesture recognizor on UICollectionView not receiving gestures

I have a ViewController with a vertically scrolling collection view that takes up the entire view. I want to be able to get swipe and pan gestures on the entire collection view (not just on cells) but I can't get any gestures. I have tried adding the gesture recognizer to the view and the collection view but neither seem to work.
Adding the gesture recognizer to the view
self.panEdgeGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
self.panEdgeGesture.delegate = self;
[self.collectionView addGestureRecognizer:self.panEdgeGesture];
[self.panEdgeGesture setEdges:UIRectEdgeRight];
Then I added these functions:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch{
return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (void)handlePan:(UISwipeGestureRecognizer *)sender
{
DebugLog(#"Received pan gesture");
}
Could the collection view cells stop the gesture events from triggering? They have no gestures themselves.
Per UIScreenEdgePanGestureRecognizer's Class Reference:
After creating a screen edge pan gesture recognizer, assign an
appropriate value to the edges property before attaching the gesture
recognizer to your view. You use this property to specify from which
edges the gesture may start. This gesture recognizer ignores any
touches beyond the first touch.
So change you code to:
self.panEdgeGesture = [[UIScreenEdgePanGestureRecognizer alloc];
[self.panEdgeGesture setEdges:UIRectEdgeRight];
initWithTarget:self action:#selector(handlePan:)];
self.panEdgeGesture.delegate = self;
[self.collectionView addGestureRecognizer:self.panEdgeGesture];

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

SWRevealViewController pan gesture recogniser issue

I am using SWRevealViewController from John-Lluch. I need to use pan gesture to view the sidebars and I am using swipe to view my previous and next articles. However, the pan gesture can only be detected.
UPDATED: My swipe gesture worked if I disable my pan gesture.
Pan Gesture
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
Swipe gesture
UISwipeGestureRecognizer *left = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft:)] autorelease];
left.direction = UISwipeGestureRecognizerDirectionLeft;
UISwipeGestureRecognizer *right = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight:)] autorelease];
right.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:left];
[self.view addGestureRecognizer:right];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
[self.view addGestureRecognizer:left];
Notice the difference? Implement the SwipeGesture as a property in your .h or make it in your .xib and link it to your .h
It is difficult to handle both pan and swipe gesture recognizer simultaneously. You will have handle the SWRevealViewController delegates for pan gesture and also swipe gesture for the current viewcontroller.
As apple suggests to differentiate the gesture you could use the following method
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Excerpt from the Apple Documentation:
This method is called when recognition of a gesture by either
gestureRecognizer or otherGestureRecognizer would block the other
gesture recognizer from recognizing its gesture. Note that returning
YES is guaranteed to allow simultaneous recognition; returning NO, on
the other hand, is not guaranteed to prevent simultaneous recognition
because the other gesture recognizer's delegate may return YES.

How can I except a part from a UIView for a gesture recognizer?

I am developing an app and I want to scroll through different UIViewControllers with a UISwipeGestureRecognizer.
Everything works, but there is one thing where I couldn't find a solution for.
I add the UIGestureRecognizers like this
- (void)viewDidLoad {
[super viewDidLoad];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeRight];
}
Now I want to know if it is possible to except a part of the self.view where it is possible to swipe to a different view.
I want to do this around a UISwitch.
Can someone please help me?
Best regards,
Lukas Goes
You can use another UIView and add it to self.view. Those UISwipeGestureRecognizer won't work inside that UIView.
Add another UISwipeGestureRecognizer to the switch, and, on your original swipe gesture recognisers, call requireGestureRecognizerToFail: on the switch's new recognizer.
Hope that makes sense, it's such a verbose class... :\
I suggest you to use the delegate method of a gesture recognizer : gestureRecognizer:shouldReceiveTouch:.
In the touch parameter, you have many information, like the position in the screen. You can use this position and test if it is above the switch frame. Return yes or no depending of if you want to trigger the selector associated to the gesture recognizer or not.

uiwebview inside uipageviewcontroller blocks tap to turn page

I have a uiwebview inside the uipageviewcontroller, I want to be able to select some text in the view so I have userInteractionEnabled set to YES, but when I do that I loose the option of turning the page by tapping, although swiping still works fine.
What's the best way to trap the UITapReconizer on the UIWebview and pass it on to the UIPageviewController?
Thanks.
UIPageviewController has a gestureRecognizers property
My guess is that one if its gesture recognizer and yours - if your using one - can't act simultaneously.
You could handle that through the gestures' methods, see question for handling multiple UITapGestureRecognizers, requiring your webView tap recognizer to fail for pageViewController's tap to succeed.
Like this (not tested)
UILongPressGestureRecognizer *yourTapGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPress:)];
for(UIGesture *gesture in yourPageController.gestureRecognizers){
[gesture requireGestureRecognizerToFail:yourTapGesture];
}
[pageControllerTap requireGestureRecognizerToFail:yourTapGesture];
Also, I don't understand why you need userInteractionEnabled, is your UIWebView taking all space ? You could attach the gesture to webView's superview, and test webView-hit with locationInView: method.
I've sorted it.
I've added a gesture recogniser to the controller generating the uiwebview:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleGesture:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.delegate = self;
[webView addGestureRecognizer:tapRecognizer];
With
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return NO;
}
And adding in the pageviewController:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}

Resources