Uiswipegesturerecognizerのセットアップのためにuiswipegesturerecognizer@selectorが呼び出されていません

StackOverflow https://stackoverflow.com//questions/22055625

質問

私の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. 同じビューでPANとスワイプを設定しないでください - 混乱を防ぐために異なるサブビューに設定できます。

  2. スワイプのための別の認識担当者を使用して、ダブルタップや2本の指タップのように切り替えるための別の認識担当者を使用してください。

  3. デリゲート方式を使う - GestureRecognizer:shouldberequiredTofailBygesturecognizer:

    たとえば、パネル/ツールバーの領域でタッチが始まっていて、スワイプを使用できるPAN認識担当者に触れている場合は、CHACOに。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top