0

I've set up my UIView to call a method when the view is panned up and to call some other method when the view is swiped up. My pan works fine and I can keep my pan disabled if my velocity.y is above a certain limit, but I can never get the swipe action to work when my pan fails. I've tried playing around with the delegate methods without much luck. Looked over this solution but no luck: https://stackoverflow.com/a/21728621/1925859

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer * panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    panRec.delegate = self;
    [panedView addGestureRecognizer:panRec];

    UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    swipeRec.delegate = self;
    [panedView addGestureRecognizer:swipeRec];

    [swipeRec requireGestureRecognizerToFail:panRec];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
    {
        UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
        CGPoint velocity = [pan velocityInView:gestureRecognizer.view];
        if (ABS(velocity.y) > 200)
        {

            NSLog(@"Swipe actiavted");
            return NO;
        }
    }
        return YES;  
}

 - (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
    NSLog(@"In Swipe");
     if(recognizer.direction == UISwipeGestureRecognizerDirectionUp)
    {
        panedView.frame =CGRectOffset( panedView.frame, 0, -1*self.view.bounds.size.height*.80);
    }
}
Community
  • 1
  • 1
Brosef
  • 2,945
  • 6
  • 34
  • 69
  • You should (a) specify direction of swipe gesture when you instantiate it (it gets you out of having to worry about the velocity of the pan); and (b) make the pan require the swipe to fail, rather than the other way around. You might not even need that delegate method anymore... – Rob May 26 '14 at 20:52

1 Answers1

2

If you remove your delegate method, add direction to the swipe and change the receiver of requireGestureRecognizerToFail then it will work. Note that pans must be way slower than sweeps in order to be recognized.

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer * panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [panedView addGestureRecognizer:panRec];

    UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

    swipeRec.direction = UISwipeGestureRecognizerDirectionUp;
    [panedView addGestureRecognizer:swipeRec];

    [panRec requireGestureRecognizerToFail:swipeRec];
}


- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
    NSLog(@"In Swipe");
    if(recognizer.direction == UISwipeGestureRecognizerDirectionUp)
    {
        panedView.frame =CGRectOffset( panedView.frame, 0, -1*self.view.bounds.size.height*.80);
    }
}

- (IBAction)handlePan:(UISwipeGestureRecognizer *)recognizer
{
    NSLog(@"PAN");
}
augustzf
  • 2,385
  • 1
  • 16
  • 22