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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top