在我的 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开始工作。

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

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

有帮助吗?

解决方案

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

为了让它们都发挥作用,让你的班级成为 delegate 对于手势并实施

– gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

回来 YES.

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

其他提示

滑动和摇动手势非常相似,导致混淆,

有一些补救措施:

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

  2. 使用另一个识别器来滑动以使双击或2个手指轻触以便切换,因为这不能被解释为平移

  3. 使用委托方法 - gesturecognizer:persberequiredtofailbybesturerecognizer:

    例如,如果触摸开始在面板/工具栏的区域中并使PAN识别器失败,则允许使用滑移识别器。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top