在我的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:

关于ios - 由于设置了UIPanGestureRecognizer,因此未调用UISwipeGestureRecognizer @selector,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22055625/

10-12 00:35
查看更多