2

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?

Oleg
  • 1,383
  • 4
  • 19
  • 35
  • 1
    What exactly do you mean by 'offset'? – Darek Rossman Dec 01 '11 at 13:36
  • If my view is a scroll view. I need to change contentOffset property by value y which I need help to calculate. I think it depends on swipe power and direction – Oleg Dec 01 '11 at 13:45
  • If your view is a scroll view, then touches will change the content offset by default...the UIScrollView already has a panGestureRecogonizer built in which is responsible for changing the content offset in the given direction...Any 'swipes' in UIScrollView will be consumed by the UIScrollView itself. If you add your own recognizer, things can get very tricky....perhaps I'm still not understanding you correctly. – Darek Rossman Dec 01 '11 at 13:51

2 Answers2

8

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.

Darek Rossman
  • 1,323
  • 1
  • 10
  • 12
8

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.

jbat100
  • 16,757
  • 4
  • 45
  • 70