376

ユーザーが2秒間押し続けたかどうかを検出しています:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                             initWithTarget:self 
                                             action:@selector(handleLongPress:)];
        longPress.minimumPressDuration = 2.0;
        [self addGestureRecognizer:longPress];
        [longPress release];

これは私が長押しを処理する方法です:

-(void)handleLongPress:(UILongPressGestureRecognizer*)recognizer{
    NSLog(@"double oo");
}

2秒以上押すと、「doubleoo」というテキストが2回印刷されます。どうしてこれなの?どうすれば修正できますか?

4

7 に答える 7

716

UILongPressGestureRecognizerは、継続的なイベント認識機能です。状態を調べて、これがイベントの開始、中間、または終了であるかどうかを確認し、それに応じて行動する必要があります。つまり、開始後にすべてのイベントを破棄するか、必要に応じて動きだけを見ることができます。クラスリファレンスから :

長押しのジェスチャは継続的です。ジェスチャは、許容される指の数(numberOfTouchesRequired)が指定された期間(minimumPressDuration)押され、タッチが許容される移動範囲(allowableMovement)を超えて移動しない場合に開始されます(UIGestureRecognizerStateBegan)。ジェスチャレコグナイザは、指が移動するたびに変更状態に移行し、いずれかの指を離すと終了します(UIGestureRecognizerStateEnded)。

今、あなたはこのように状態を追跡することができます

-  (void)handleLongPress:(UILongPressGestureRecognizer*)sender { 
    if (sender.state == UIGestureRecognizerStateEnded) {
      NSLog(@"UIGestureRecognizerStateEnded");
    //Do Whatever You want on End of Gesture
     }
    else if (sender.state == UIGestureRecognizerStateBegan){
       NSLog(@"UIGestureRecognizerStateBegan.");
   //Do Whatever You want on Began of Gesture
     }
  }
于 2010-07-23T16:41:05.767 に答える
119

UILongPressGestureRecognizerの状態を確認するには、セレクターメソッドにifステートメントを追加するだけです。

- (void)handleLongPress:(UILongPressGestureRecognizer *)sender {    
    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Long press Ended");
    } else if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"Long press detected.");
    }
}
于 2010-11-11T15:43:17.707 に答える
78

状態ごとに動作が異なるため、正しい状態を確認する必要があります。UIGestureRecognizerStateBeganほとんどの場合、。を含む状態が必要になりますUILongPressGestureRecognizer

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                             initWithTarget:self 
                                             action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[myView addGestureRecognizer:longPress];
[longPress release];

..。

- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
    if(UIGestureRecognizerStateBegan == gesture.state) {
        // Called on start of gesture, do work here
    }

    if(UIGestureRecognizerStateChanged == gesture.state) {
        // Do repeated work here (repeats continuously) while finger is down
    }

    if(UIGestureRecognizerStateEnded == gesture.state) {
        // Do end work here when finger is lifted
    }
}
于 2011-08-18T20:55:26.003 に答える
19

これを試してみてください:

Objective-C

- (void)handleLongPress:(UILongPressGestureRecognizer*)sender { 
    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Long press Ended");
    } else if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"Long press detected.");
    }
}

Swift 2.2:

func handleLongPress(sender:UILongPressGestureRecognizer) {

        if (sender.state == UIGestureRecognizerState.Ended) {
            print("Long press Ended");
        } else if (sender.state == UIGestureRecognizerState.Began) {
            print("Long press detected.");
        }
}
于 2015-09-07T08:38:51.223 に答える
17

Swift 3.0:

func handleLongPress(sender: UILongPressGestureRecognizer) {

    if sender.state == .ended {
        print("Long press Ended")
    } else if sender.state == .began {
        print("Long press detected")
    }
于 2016-11-26T12:49:53.280 に答える
14

Swiftで処理する方法は次のとおりです。

func longPress(sender:UILongPressGestureRecognizer!) {

        if (sender.state == UIGestureRecognizerState.Ended) {
            println("Long press Ended");
        } else if (sender.state == UIGestureRecognizerState.Began) {
            println("Long press detected.");
        }
}
于 2014-09-24T21:29:13.333 に答える
6

ジェスチャハンドラは、ジェスチャの状態ごとに呼び出しを受け取ります。したがって、各状態をチェックして、コードを必要な状態にする必要があります。

if-elseよりもswitch-caseの使用を好む必要があります:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                         initWithTarget:self 
                                         action:@selector(handleLongPress:)];
    longPress.minimumPressDuration = 1.0;
    [myView addGestureRecognizer:longPress];
    [longPress release];

-(void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
        switch(gesture.state){
          case UIGestureRecognizerStateBegan:
               NSLog(@"State Began");
               break;
          case UIGestureRecognizerStateChanged:
               NSLog(@"State changed");
               break;
          case UIGestureRecognizerStateEnded:
               NSLog(@"State End");
               break;
          default:
               break;
         }
}
于 2014-04-04T09:21:17.270 に答える