整个视图的UIGestureRecognizer

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];
5个回答

4

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

我希望这能解决你的问题。而在“touches enabled”中,将计数器设置为2或任何触摸次数即可。 :) 如果你有任何疑问,请随时提出。

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等。 - Ezeki

1

在IT相关的内容中,翻译为中文:

添加另一个视图,使用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];

问题在于,它会阻塞所有其他按钮和元素。您无法在表格视图上滚动、触摸或进行任何操作,也无法在其下方进行任何操作。 - Neil

0

有两种方法可以让UIView检测touch。你想要的方法是使用UIGestureRecognizer,这个答案已经由Ezeki给出了。 另一种方法是重写UITouch代理来检测touch

- (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.)
    }
}

使用这种方法,你如何识别滑动手势? - Neil
@SirKaydian,还有一个方法 - (void) touchesMove:(NSSet *)touches withEvent:(UIEvent *)event { },你可以检查 coords 来检测 swipe - Hemang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接