programing

를 누를 때 UILongPressGestureRecognizer가 두 번 호출됩니다.

sourcejob 2023. 5. 29. 10:37
반응형

를 누를 때 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는 연속 이벤트 인식기입니다.이것이 이벤트의 시작인지 중간인지 끝인지 상태를 보고 그에 따라 행동해야 합니다. 즉, 시작 후 모든 이벤트를 버리거나 필요에 따라 움직임만 볼 수 있습니다.클래스 참조:

길게 누르는 제스처는 연속적입니다.허용 가능한 손가락 수(Number Of)가 시작되면 제스처가 시작됩니다(UIGesture RecognizerStateBegin).터치 필요)를 지정된 기간(최소 누름 기간) 동안 눌렀으며, 터치가 허용되는 이동 범위(가능한 이동)를 벗어나지 않습니다.제스처 인식기는 손가락이 움직일 때마다 변경 상태로 전환되며, 손가락 중 하나를 들어올리면 종료됩니다(UIGesture RecognizerStateEnded).

이제 다음과 같은 상태를 추적할 수 있습니다.

-  (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.");
    }
}

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.");
        }
}

Swift 3.0:

func handleLongPress(sender: UILongPressGestureRecognizer) {

    if sender.state == .ended {
        print("Long press Ended")
    } else if sender.state == .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.");
        }
}

제스처 핸들러가 각 제스처 상태에 대한 호출을 수신합니다.따라서 각 상태를 확인하고 코드를 필수 상태로 만들어야 합니다.

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

언급URL : https://stackoverflow.com/questions/3319591/uilongpressgesturerecognizer-gets-called-twice-when-pressing-down

반응형