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

Eug*_*din 0 ios uiswipegesturerecognizer uipangesturerecognizer

在我的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];
           }
        }
     }
 }
Run Code Online (Sandbox Code Playgroud)

所以问题是handleSwipe永远不会被调用...当我注释掉UIPanGestureRecognizer设置时,handleSwipe开始工作.

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

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

Wai*_*ain 5

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

为了使它们都起作用,让你的课程delegate成为手势并实施

– gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

返回YES.

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