2014-02-26 19 views
0

在我的iOS应用程序,我有以下设置:UISwipeGestureRecognizer @selector不会被调用,因为UIPanGestureRecognizer设立

- (void)setupGestures 
{ 
    UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];               
    [self.view addGestureRecognizer:panRecognizer]; 

    UISwipeGestureRecognizer* swipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; 

    [swipeUpRecognizer setDirection:UISwipeGestureRecognizerDirectionUp]; 
    [self.view addGestureRecognizer:swipeUpRecognizer]; 
} 

// then I have following implementation of selectors 

// this method supposed to give me the length of the swipe 

- (void)panGesture:(UIPanGestureRecognizer *)sender 
{ 
    if (sender.state == UIGestureRecognizerStateBegan) 
    { 
     startLocation = [sender locationInView:self.view]; 
    } 
    else if (sender.state == UIGestureRecognizerStateEnded) 
    { 
     CGPoint stopLocation = [sender locationInView:self.view]; 
     CGFloat dx = stopLocation.x - startLocation.x; 
     CGFloat dy = stopLocation.y - startLocation.y; 
     CGFloat distance = sqrt(dx*dx + dy*dy); 
     NSLog(@"Distance: %f", distance); 
    } 
} 

// this method does all other actions related to swipes 

- (void)handleSwipe:(UISwipeGestureRecognizer *)gestureRecognizer 
{ 
UISwipeGestureRecognizerDirection direction = [gestureRecognizer direction]; 
    CGPoint touchLocation = [gestureRecognizer locationInView:playerLayerView]; 

    if (direction == UISwipeGestureRecognizerDirectionDown && touchLocation.y > (playerLayerView.frame.size.height * .5)) 
    { 
     if (![toolbar isHidden]) 
     { 
      if (selectedSegmentIndex != UISegmentedControlNoSegment) 
      { 
       [self dismissBottomPanel]; 
      } 
      else 
      { 
      [self dismissToolbar]; 
      } 
     } 
    } 
} 

所以问题是,handleSwipe是从来没有得到所谓的...当我注释掉UIPanGestureRecognizer设置,handleSwipe开始工作。

我很新手势识别编程,所以我假设我在这里失去了一些基本的东西。

任何形式的帮助,高度赞赏!

+1

附注 - 为什么要调用'allocWithZone:nil'?只需调用'alloc'。 – rmaddy

+0

当然......但这并不影响我的问题,对吧? :) –

+0

完全没有,这就是为什么我用“旁注”作为前缀。 :) – rmaddy

回答

5

你需要告诉手势如何互相交流。这可以通过让它们同时运行来完成(默认是不会的),或者通过设置一个只在另一个失败时才工作。

为了让他们都工作,让您的课delegate的手势和实施

– gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

返回YES

要设置其中一个仅在另一个失败时才起作用,请使用requireGestureRecognizerToFail:

+0

谢谢!我只是做了你的建议,但handleSwipe仍然没有被调用:( –

+0

我添加了UIGestureRecognizerDelegate到我的课,但 - gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:没有得到调用:( –

+0

我的愚蠢...忘了这样做:panRecognizer。委托=自我; –

0

挥击和平移手势非常相似,引起混乱,

有一些补救措施:

  1. 不要设置平移和轻扫在相同的观点 - 它可以设置在不同的子视图上防止混淆。

  2. 使用另一个识别器用于滑动切换等的双击或2-手指敲击因为这不能被解释为锅

  3. 使用委托方法 - gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer:

    例如如果触摸开始于面板/工具栏的区域并且不能通过平移识别器允许使用滑动来查找。

相关问题