Detect sliding up gesture on iOS - 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?

Related

How to synchronise UIView moving with swipe gesture of UIPageViewController

Ok, here is the thing:
I need to have few animations happening on every screen change in my PageViewController.
So, when a user swipes, an image should fly in from the top left corner, from example.
I can make that animation to happen over time when user swipes and changes the page, but what I need is to that animation to be synchronised with the swipe movement itself.
So if a user presses the screen and starts swiping, the animation should follow user's finger and animate it's translation with the finger movement.
How can I achieve that?
I guess I need some sort of swipe gesture listener, but I failed to find any solution online. I guess I'm not using the right keywords.
You can use a UIPanGestureRecognizer to get the number of pixels swiped:
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panRecognizer_Panned:)];
[self.myViewToSwipe addGestureRecognizer:panRecognizer];
...
- (void) panRecognizer_Panned:(UIPanGestureRecognizer *)recognizer {
CGFloat pixelsMovedHoriz = [recognizer translationInView:self.vwRelativeTo].x;
}

How to detect swipe direction?

I would like to detect when a user makes a swipe gesture, and detect it's direction (left, right, up or down). I need to detect it not when the gesture is finished, but just when the iPhone knows the direction, even if it's not finished.
I am using XCode 5, and designing for iPhone 5 with iOS 7.
I would like to know the code that I have to paste to the .h, and to the .m, and if I have to drag and drop any item to the mainView.
If you want to know the direction when the gesture is not finished you would probably need the UIPanGestureRecognizer and detect the direction using the velocityInView: method.
in the *.h file:
#interface ViewController : UIViewController
#property (nonatomic, strong) UIPanGestureRecognizer *recognizer;
#end
in the *.m file:
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panGesture:)];
[self.view addGestureRecognizer:self.recognizer];
}
-(void)panGesture:(UIPanGestureRecognizer *)sender
{
NSLog(#"Velocity: %#", NSStringFromCGPoint([sender velocityInView:self.view]));
// Here You need to determine the direction
}
#end
From Docs:
Gestures are either discrete or continuous. A discrete gesture, such
as a tap, occurs once. A continuous gesture, such as pinching, takes
place over a period of time. For discrete gestures, a gesture
recognizer sends its target a single action message. A gesture
recognizer for continuous gestures keeps sending action messages to
its target until the multitouch sequence ends,
So if you need to "detect it not when the gesture is finished, but just when the iphone knows the direction, even if it's not finished" you could not use UISwipeGestureRecognizer since it is a discrete one. You should use UIPanGestureRecognizer and analyse it is results to check is there a swipe gesture or not.
To have a good example how it works, you can download a sample code provided by Apple:
https://developer.apple.com/library/ios/samplecode/SimpleGestureRecognizers/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009460
Hope that helps ;)
You can easily get using UISwipeGestureRecognizers property directions...
After :----
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeRight)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.yourView addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeLeft)];
swipeRight.direction = UISwipeGestureRecognizerDirectionLeft;
[self.gifWebView addGestureRecognizer:swipeLeft];
-(void)swipeRight{
//Do whatever you want
}
-(void)swipeLeft{
//Do whatever you want
}
And For Before Swipe you can use UIPanGestureRecognizer
I hope it help you
Swipe gestures are very quick. Swipe gesture recognizers are designed to fire once, not continuously. I don't think you will be able to get information about a swipe gesture that is "in flight" very easily.
If this is urgent, you will probably have to create a custom subclass of swipe gesture recognizer and add additional logic and delegate messages that would notify the delegate once the gesture recognizer decides that it has detected a swipe. That is way beyond your current skill level however.

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.

Custom GestureRecognizer Creation

I want to create custom Gesture Recognizer.
For Example:
if user pan the screen from top to bottom some distance from there if the finger move left direction some distance, i need to invoke some method from view. same if the finger move right direction means some other function should get invoke.
For this functionality
1. can i use UIPanGestureRecognizer and detect the user interaction throw the translation position,
2.There is any way to implement my own custom GestureRecognizer
(like: UIPanLeftLGestureRecognizer for user drag the finger from top to bottom some distance and moved left from there, UIPanRightLGestureRecognizer for user drag the finger from top to bottom some distance and moved right from there.)
Which is the best way to achieve the solution for this problem. kindly guide me.
Thanks in advance.
I would use two UISwipeGestureRecognizers with different directions like this:
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipedDown:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipedRight:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
And don't forget to add these gesture recognizers to appropriate view

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

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.

Resources