6

我希望能够制作一个手势识别器来监听屏幕上任何地方的两指滑动。

现在我在一个滚动的子视图中有一个 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];
4

5 回答 5

4

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

iPad 应用程序的 UIScrollview 中的两指轻扫

于 2012-10-17T00:20:17.183 回答
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];

希望这会有所帮助

于 2012-10-12T20:56:26.817 回答
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 或任意数量的触摸.. :) 如果您有任何疑问,请随时询问

于 2012-10-17T13:31:08.880 回答
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];
于 2012-10-15T13:54:38.073 回答
0

有两种UIView检测方法touch。您定义并想要的方法是EzekiUIGestureRecognizer已经给出的答案。您可以用来检测的另一种方法是覆盖委托。touchUITouch

- (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.)
    }
}
于 2012-10-15T13:17:55.223 回答