ios UISwipeGestureRecognizer calculate offset

I'm adding swipe gesture recognizer to my application

- (void)createGestureRecognizers

    //adding swipe up gesture
    UISwipeGestureRecognizer *swipeUpGesture= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeUpGesture:)];
    [swipeUpGesture setDirection:UISwipeGestureRecognizerDirectionUp];
    [self.view addGestureRecognizer:swipeUpGesture];
    [swipeUpGesture release];

And the method to handle swipe events:

-(IBAction)handleSwipeUpGesture:(UISwipeGestureRecognizer *)sender
    NSLog(@"handleSwipeUpGesture: called");

How can I calculate offset here? to move the view?


The UIGestureRecognizer abstract superclass for UISwipeGestureRecognizer has the foollowing methods

- (CGPoint)locationInView:(UIView *)view
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView *)view

Which allow you to know the position of the gesture in the view, but this is a discrete gesture recognizer (which will fire at a given "translation" or "offset" whatever you want to call it, which you cannot control). It sounds like you are looking for continuous control, for this you want a UIPanGestureRecognizer which has the following methods (which do the translation calculations for you)

- (CGPoint)translationInView:(UIView *)view
- (void)setTranslation:(CGPoint)translation inView:(UIView *)view
- (CGPoint)velocityInView:(UIView *)view

You will then get quick fire callbacks while the gesture is continuously unfolding.

UISwipeGestureRecognizer is for detecting a discrete swipe gesture - it only fires your action one time after a swipe has completed - so if youre asking about the offset or distance the finger moved, you'd probably want to look at creating a subclass of UIGestureRecognizer or using UIPanGestureRecognizer to get continuous gesture info. Not sure about what exactly you're looking to do, but a UIScrollView might be in order as well...

Check out the apple docs for gesture recognizers.

Need Your Help

How to make exe run as scheduled task

c# .net winforms scheduled-tasks exe

I created a WinForm project in c# and added a code file called Loader.cs. In that file I have a method called LoadData(). I deleted the default "Form1" from the project changed the logic in the M...

Apache CXF WSDLToJava Error: operation already exists. How to bypass?

wsdl cxf wsdl2java

I am developing second client application that is consuming existing SOAP web-services.