整个视图中的UIGestureRecognizer

时间:2012-10-12 20:32:30

标签: iphone objective-c ios

我希望能够让手势识别器在屏幕上的任何位置聆听双指滑动。

现在我在子视图中有一个UITableView,当然滚动,它似乎没有拿起我在视图上设置的UIGestureRecognizer。当你使用两个手指时,它只会滚动...

有没有办法制作整个视图,也许是超级视图,听一个双指触摸,当它识别出来时,它会不是滚动表格视图,做我想做的事情?

编辑:这是我的代码,但这是一个非常笼统的问题。我只想在整个视图中拥有UIGestureRecognizer,整个事情。

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];

5 个答案:

答案 0 :(得分:4)

也许我没有清楚地解释我的问题。但是对于我所做的事情,这最终会起作用,也许它将来会帮助其他人:

Two finger swipe in UIScrollview for iPad application

答案 1 :(得分:2)

你可以尝试这样做:

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];

希望有所帮助

答案 2 :(得分:2)

//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...
        }
    }

我希望这能解决你的问题。并且在触摸启用时使计数等于2或任意数量的触摸.. :)随意询问您是否有任何疑问

答案 3 :(得分:1)

在self.view上添加另一个带有self.view框架的视图,如“viewForGesture”,并在该视图上添加手势识别器,如

       UIView *viewForGesture = [[ [UIView alloc]initWithFrame:self.view.frame] autorelease];
       [viewForGesture setBackgroundColor:[UIColor clearColor]];
       [self.view addSubview:viewForGesture];
        .
        .
        .
       [viewForGesture addGestureRecognizer:swipeOpen];
       [viewForGesture addGestureRecognizer:swipeClose];

答案 4 :(得分:0)

有两种方法可以UIView检测touch。你定义的方法,并希望它是UIGestureRecognizer Ezeki 已经给出了答案。  您可以用来检测touch的另一种方法是覆盖UITouch个委托。

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];

    if(touch.tapCount==2)
    {
        //Do something ... (Here, you can also check for the object which touched.)
    }
}