Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIGestureRecognizer across entire view

I want to be able to make a gesture recognizer listening for a two finger swipe, anywhere on the screen.

Right now I have a UITableView in a sub-view that scrolls, of course, and it doesn't seem to pick up the UIGestureRecognizer that I set on the view. When you use the two fingers, it just scrolls...

Is there a way to make the entire view, maybe superview, listen for a two finger touch and when it recognizes it, it will instead of scrolling the table view, do what I want it to do?

EDIT: Heres my code but this is suposed to be a very general question. I just want to be able to have UIGestureRecognizers across the entire view, the whole thing.

navBarTitle.title = @"Crunch";

//opening the menuView from launch

//setup the customtable view as a subview in menuView
SDMenuViewController *mvc = [[[SDMenuViewController alloc] initWithNibName:@"SDNestedTableView" bundle:nil] autorelease];
[self addChildViewController:mvc];
[mvc didMoveToParentViewController:self];
[menuView addSubview:mvc.view];

[mvc.view setFrame:CGRectMake(mvc.view.frame.origin.x, mvc.view.frame.origin.y, mvc.view.frame.size.width, mvc.view.frame.size.height + 44)]; //add navBarHeight, for some strage reason

//add swipe down gesture on for the menu

UISwipeGestureRecognizer *swipeClose =[[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(closeMenu)] autorelease];
 swipeClose.numberOfTouchesRequired = 2;
 swipeClose.direction = UISwipeGestureRecognizerDirectionUp;

 UISwipeGestureRecognizer *swipeOpen = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(openMenu)] autorelease];
 swipeOpen.numberOfTouchesRequired = 2;
 swipeOpen.direction = UISwipeGestureRecognizerDirectionDown;

for (UIGestureRecognizer* rec in menuView.gestureRecognizers) {
    [rec requireGestureRecognizerToFail:swipeClose];
    [rec requireGestureRecognizerToFail:swipeOpen];
}

for (UIGestureRecognizer* rec in mvc.view.gestureRecognizers) {
    [rec requireGestureRecognizerToFail:swipeClose];
    [rec requireGestureRecognizerToFail:swipeOpen];
}

[mvc.view addGestureRecognizer:swipeClose];
[mvc.view addGestureRecognizer:swipeOpen];

[self.view addGestureRecognizer:swipeClose];
[self.view addGestureRecognizer:swipeOpen];
like image 945
Neil Avatar asked Oct 12 '12 20:10

Neil


4 Answers

Maybe I did not explain my question clearly. But for what I was doing this ended up working, and maybe it will help other people in the future:

Two finger swipe in UIScrollview for iPad application

like image 115
Neil Avatar answered Sep 28 '22 18:09

Neil


you can try to do something like this:

UISwipeGestureRecognizer* p = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(actionPan:)];
p.numberOfTouchesRequired = 2;

for (UIGestureRecognizer* rec in tableView.gestureRecognizers) {
    [rec requireGestureRecognizerToFail:p];
}

[self.view addGestureRecognizer:p];

hope that will help

like image 30
Ezeki Avatar answered Sep 28 '22 19:09

Ezeki


//In view did load        
    [self.view setMultipleTouchEnabled:YES];

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    if ([[event touchesForView:self.view] count] > 1) {
        NSLog(@"%d active touches",[[event touchesForView:self.view] count]) ;
 UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
            swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
            [self.view addGestureRecognizer:swipeGesture];
  }

}

    -(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender 
    {
        //Gesture detect - swipe up/down , can be recognized direction
        if(sender.direction == UISwipeGestureRecognizerDirectionUp)
        {
            // do some thing...
        }
    }

i Hope this would solve your problem. and in touches enabled make the count equal to 2 or any number of touches.. :) feel free to ask if you have any doubts

like image 44
Vinod ram Avatar answered Sep 28 '22 17:09

Vinod ram


  Add another view with frame of self.view over self.view like "viewForGesture" and add gesture recognizer on that view like

       UIView *viewForGesture = [[ [UIView alloc]initWithFrame:self.view.frame] autorelease];
       [viewForGesture setBackgroundColor:[UIColor clearColor]];
       [self.view addSubview:viewForGesture];
        .
        .
        .
       [viewForGesture addGestureRecognizer:swipeOpen];
       [viewForGesture addGestureRecognizer:swipeClose];
like image 27
Dipak Chaudhari Avatar answered Sep 28 '22 18:09

Dipak Chaudhari