由于UIPanGestureRecognizer设置,未调用UISwipeGestureRecognizer @selector

时间:2014-02-26 23:16:16

标签: ios uipangesturerecognizer uiswipegesturerecognizer

在我的iOS应用中,我进行了以下设置:

- (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开始工作。

我对手势识别编程很陌生,所以我假设我在这里缺少一些基本的东西。

非常感谢任何形式的帮助!

2 个答案:

答案 0 :(得分:5)

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

要使它们都有效,请将您的课程设为delegate以获取手势并实施

– gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

返回YES

要设置一个仅在另一个失败时才能工作,请使用requireGestureRecognizerToFail:

答案 1 :(得分:0)

滑动和平移手势非常相似,导致混乱,

有一些补救措施:

  1. 不要在同一视图上设置平移和滑动 - 可以在不同的子视图上设置它以防止混淆。

  2. 使用另一个识别器进行滑动可以像双击或2指点击一样切换,因为这不能解释为平底锅

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

    例如,如果在面板/工具栏的区域中开始触摸并且无法使用平移识别器来允许滑动,则可以使用chaco。