3

アップルのドキュメントから

スワイプは個別のジェスチャであるため、関連するアクション メッセージはジェスチャごとに 1 回だけ送信されます。

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event 

UISwipeGestureRecognizer を使用しても呼び出されません

ユーザーが指を離したことを検出するにはどうすればよいですか?

4

4 に答える 4

3

UISwipeGestureRecognizer を使用してスワイプを検出する代わりに、イベント処理を使用して自分で検出しました。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    self.initialPosition = [touch locationInView:self.view];

 }
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
     UITouch *touch = [touches anyObject];
     CGPoint movingPoint = [touch locationInView:self.view];
     CGFloat moveAmt = movingPoint.y - self.initialPosition.y;
     if (moveAmt < -(minimum_detect_distance)) {
       [self handleSwipeUp];
     } else if (moveAmt > minimum_detect_distance) {
       [self handleSwipeDown];
     }
  }
 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
      [self reset];

  }
 -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
     [self reset];
  }

UIGestureRecognizer をサブクラス化していませんが、必要なビュー コントローラーでのみイベント処理を行いました。reset メソッドでは、ビュー コントローラーに属するいくつかの変数、カウンター、タイマーをリセットしています。

于 2013-11-11T04:22:34.787 に答える
2

ジェスチャ認識エンジンの状態プロパティを調べる必要があると思います。

- (void)swipe:(UISwipeGestureRecognizer *)recognizer
{
   CGPoint point = [recognizer locationInView:[recognizer view]];
   if (recognizer.state == UIGestureRecognizerStateBegan)
       NSLog(@"Swipe began");
   else if (recognizer.state == UIGestureRecognizerStateEnded)
       NSLog(@"Swipe ended");
}
于 2013-11-11T04:27:49.393 に答える
0

scrollView を使用すると、その contentOffset を検出できます

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y < -100 { // how you needed
        // do what you need
    }
}
于 2019-08-26T19:16:25.747 に答える