iOS: Can I intercept swipes on top of a UIWebView? - ios

I want the user to be able to swipe left, right, upward and downward on a UIWebview, but I don't want the HTML/Javascript to process those swipes-- I want to do that myself.
Is that possible: To intercept just the swipes? I want the taps still to go through to the UIWebview.
Thanks.

Sure thing.Just attach UIGestureRecognizer subclass to that view and hold on for calls...
UISwipeGestureRecognizer* leftSwipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(someAction)];
leftSwipeRecognizer.numberOfTouchesRequired = 1;
leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
leftSwipeRecognizer.cancelsTouchesInView = YES;
[self.webView addGestureRecognizer:leftSwipeRecognizer];
there is some sample for left swipe gesture. You can set more with very similar approach...Hope that helps.

If you add a UISwipeGestureRecognizer onto the UIWebview with
addGestureRecognizer:
it will allow you to get those events. I believe the default is to only allow one recognizer at a time, but I'm not sure if that applies to individual gesture types or across all gesture types, so this may only be the first step in solving your problem.

Related

Multiple UIGestureRecognizers in Xcode/Swift

Right now, I have two different UILabels each with their own long press and pan UIGestureRecognizers (setup through the storyboard). My final goal is to have each UILabel change color when long pressed, and without lifting their finger to end the long press, to change the value of the UILabel itself when the user pans up and down or side to side.
Right now, each UILabel has its own pan gesture method and long press gesture method. Is there any way to have a single long press/pan method for both UILabels but also have the ability to do something for one label and something else for another?
Also, is there a better approach to doing this? Eventually, I would also like to implement visual feedback when changing the value of the labels, such as in the form of animations.
I am new to iOS programming and programming in general and detailed answers are greatly appreciated. Thanks.
You can have a single function.No need to have seperate gestures for seperate Label.Example
//First add tag value to ur labels
label_one.tag=1;
label_two.tag=2;
UIPanGestureRecognizer * _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:#selector(handlePanGesture:)];
_panGestureRecognizer.delegate = self;
[label_one addGestureRecognizer:_panGestureRecognizer];
UIPanGestureRecognizer * _panGestureRecognizer_two = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:#selector(handlePanGesture:)];
_panGestureRecognizer_two.delegate = self;
[label_two addGestureRecognizer:_panGestureRecognizer_two];
-(void)handlePanGesture:(UIPanGestureRecognizer*)sender{
if(sender.tag==1){
}
else if(sender.tag==2){
}
}
Same goes for other gesture as well

Give swipe gesture priority over button

I have two UIButtons in a view (one is YES, the other NO). I now want to add a "did not answer", which would be indicated by a downward swipe on the view.
The problem is that the user may swipe down on the view, but in the process hit one of the buttons. When this happens I want to ignore the button press if there was a swipe underway. If it is just a tap on a button, the answer is recorded.
So, if the swipe occurs, I want to call the swipe gesture's action method. If it is determined no swipe occurred but one of the two buttons was touched I want to call their respective action methods. But if a button was touched in the process of a swipe, I want to call only the swipe gesture's action method.
I know there is a way but I'm wondering whether there is an EASY way of doing this. TIA for suggestions.
Since UISwipeGestureRecognizer is a "discrete" gesture, it just triggers a single action when recognized and it won't allow you to detect the end of the gesture.
So to prevent other touches during the gesture, I'd recommend using a UIPanGestureRecognizer instead since it can track your gesture from beginning to end. Then you can try setting your gesture's cancelsTouchesInView property to YES to cancel all other touches in the view that happen while that pan gesture is recognized, ex:
gesture.cancelsTouchesInView = YES;
gesture.delaysTouchesBegan = YES;
Your buttons should be registering touch up in view, so that someone who taps on the button can drag off if they decide not to proceed.
For your other swipe gesture, your buttons will not register a touch up in view during a swipe gesture, even if the swipe passes over the button or ends on it.
Okay, this seems to work. On the gesture recognizer, I used:
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipeDown:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
swipeDown.delaysTouchesBegan = YES;
swipeDown.delegate = self;
[self addGestureRecognizer:swipeDown];
They delaysTouchesBegan = YES allows it to determine whether the swipe has occurred before passing the touches on to the buttons. So, if you swipe, it calls the swipe GR, and if you touch either button, you get that. Thanks for your answers...

Single Finger Swipe Gesture only working with Pinch Gesture

I'm trying to add a very simple swipe up gesture to one of my views. The gesture does not work at all unless I apply an outward/opposite pinch gesture with two of my fingers. I am creating the gesture connections via IB. I have set up the gesture properties to Swipe: Down and Touches: 1. However it will only work with a 2 finger outward pinching motion? What is going on? Thanks.
Edit:
Ok, I've tried coding it out rather than using interface builder and I'm getting the same results. Here is the code I'm using
UISwipeGestureRecognizer *swipeUpOnVideoView = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(SwipeUp:)];
swipeUpOnVideoView.direction = UISwipeGestureRecognizerDirectionUp;
swipeUpOnVideoView.numberOfTouchesRequired = 1;
[self.playerView addGestureRecognizer:swipeUpOnVideoView];
As you can see I am specifically designating only 1 touch, however my SwipeUp: method will only execute if I use an outward pinching motion with two fingers???
check this it working proper in my application:
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeUp:)];
[swipeUp setDirection:UISwipeGestureRecognizerDirectionUp ];
[self.view addGestureRecognizer:swipeUp];
- (IBAction)swipeUp:(UISwipeGestureRecognizer *)recognizer
{
// do you stuff here
}
May be it will help you.
Happy coding...:)
It turned out I had a number of other UITapGestureRecognizers that were on my Xib file. Even though I wasn't calling them in the code anywhere they were associated with views on my page via the connections inspector. For some reason this interference made it so that my single finger swipeGesture would only register if I used a two finger pinching motion.
Deleting multiple UITapGestureRecognizers from my xib and then writing the code for the UISwipeGestureRecognizer programmatically fixed the problem for me.

IOS: Detect swipe gestures in a specific place on a UIView

I am creating a storyboard app where the view is changed when the user performs swipe gestures. The issue I am having is that when you drag and drop a gesture recognizer onto the view from themain.storyboard file, the gesture is recognized from anywhere inside the UIView. Basically, I need to recognize a gesture that is performed in a specific area on the screen, similar to how you drag down the notification center in IOS 6. If this is unclear or you need more details, feel free to ask.
Thanks in advance for any help!
I am not sure if this will help you or you tried something like this, but i want to share my idea.
You can try UISwipeGestureRecognizer in your ViewControl.m like below:
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeToDoMethod)];
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionRight];
[[self innerView] addGestureRecognizer: swipeGesture];
You can add an inner view into your main view and add this gesture to that view.
Hope this helps you, good luck! :)
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(SwipeView)];
[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self innerView] addGestureRecognizer: swipe];
//UISwipeGestureRecognizerDirectionRight (or) up (or) down
Use another UIView with 0 visibility, put it on area where you want recognize a gesture and then add gesture to this UIView.
This is just an idea.
Hope this will help you.
You can add a subview into your view, set the subview's frame to the area where you want to catch the swipe gesture, add recognizer to it, and set its background color to clear color. Hope it help.
Adding Swift version:
//Add gesture recognizer
let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(dismissShowMessageAndButtonDueToSwipeUp))
swipeGesture.direction = .up
backgroundView.addGestureRecognizer(swipeGesture)

Detect sliding up gesture on iOS

Which gesture am I looking to capture if I want to detect when the user slides his/her finger upwards (think of scrubbing through a song, that kind of motion, but up) and adjust a setting as a result (I don't want to use a UISlider).
What would you guys recommend for this? Is that a pan gesture recognizer or a swipe?
Try this:
UISwipeGestureRecognizer *swipeRecognizer =
[[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(swipeDetected)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeRecognizer];
A swipe is one-time simple motion; observe that it happened, and you respond afterwards. A pan gesture is a continuous movement that you want to able to track while it happens. Which of those do you want?

Resources