【问题标题】:UIGestureRecognizer across entire view整个视图中的 UIGestureRecognizer
【发布时间】:2012-10-12 20:32:30
【问题描述】:

我希望能够让手势识别器监听屏幕上任意位置的两指滑动。

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

有没有办法让整个视图,也许是超级视图,监听两根手指的触摸,当它识别到它时,它不会滚动表格视图,而是做我想让它做的事情?

编辑:这是我的代码,但这应该是一个非常普遍的问题。我只是希望能够在整个视图中使用 UIGestureRecognizers。

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

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案1】:

    也许我没有清楚地解释我的问题。但是对于我所做的事情,这最终奏效了,也许它会在未来帮助其他人:

    Two finger swipe in UIScrollview for iPad application

    【讨论】:

      【解决方案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];
      

      希望对你有帮助

      【讨论】:

      • 当然,因为它首先尝试捕捉滑动。如果您想在点击之前捕捉双击,则同样的事情,您的点击会延迟一段时间,直到双击时间到期。这就是你支付它的方式。您也可能想尝试使用pan 而不是swipe 甚至是UIView 的触摸检测方法,如touchesBegan 等。
      【解决方案3】:
      //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 或任意数量的触摸.. :) 如果您有任何疑问,请随时询问

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 这个问题,是阻止所有其他按钮和东西。您不能滚动、触摸或 tableview 上的任何东西,或下面的任何东西。
        【解决方案5】:

        有两种方法可以让UIView 检测touch。您定义和想要的方法是UIGestureRecognizerEzeki 已经给出了答案。 您可以用来检测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.)
            }
        }
        

        【讨论】:

        • 你如何识别使用这种方法的滑动?
        • @SirKaydian,还有另外一种方法- (void) touchesMove:(NSSet *)touches withEvent:(UIEvent *)event { } 你可以检查coords 来检测swipe
        猜你喜欢
        • 2011-03-01
        • 1970-01-01
        • 2012-04-26
        • 2011-11-17
        • 1970-01-01
        • 1970-01-01
        • 2011-06-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多