[iphone] 아래로 누를 때 UILongPressGestureRecognizer가 두 번 호출됩니다.

사용자가 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 초 이상 누르면 텍스트 “double oo”가 두 번 인쇄됩니다. 왜 이런거야? 어떻게 고칠 수 있습니까?



답변

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
     }
  }


답변

UILongPressGestureRecognizer의 상태를 확인하려면 선택기 메소드에 if 문을 추가하십시오.

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


답변

각 상태마다 다른 동작이 있으므로 올바른 상태를 확인해야합니다. 의 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
    }
}


답변

이것을 시도하십시오 :

목표 -C

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

스위프트 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.");
        }
}


답변

Swift에서 처리하는 방법은 다음과 같습니다.

func longPress(sender:UILongPressGestureRecognizer!) {

        if (sender.state == UIGestureRecognizerState.Ended) {
            println("Long press Ended");
        } else if (sender.state == UIGestureRecognizerState.Began) {
            println("Long press detected.");
        }
}


답변

스위프트 3.0 :

func handleLongPress(sender: UILongPressGestureRecognizer) {

    if sender.state == .ended {
        print("Long press Ended")
    } else if sender.state == .began {
        print("Long press detected")
    }


답변

제스처 처리기는 각 제스처 상태에 대한 전화를받습니다. 따라서 각 상태를 확인하고 필요한 상태로 코드를 넣어야합니다.

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;
         }
}