programing

UISwip 제스처 인식기의 방향 설정

lovejava 2023. 6. 28. 21:13

UISwip 제스처 인식기의 방향 설정

제 뷰 기반 아이폰 프로젝트에 간단한 스와이프 제스처 인식을 추가하고 싶습니다.모든 방향(오른쪽, 아래, 왼쪽, 위)의 제스처를 인식해야 합니다.

UISwipGesture Recognizer에 대한 문서에 나와 있습니다.

비트 OR 피연산자를 사용하여 여러 UISwipGestureRecognizerDirection 상수를 지정하여 여러 방향을 지정할 수 있습니다.기본 방향은 UISwipGestureRecognizerDirectionRight입니다.

하지만 저에게는 그것이 작동하지 않습니다.네 방향이 모두 OR'되면 왼쪽과 오른쪽 스위프만 인식됩니다.

- (void)viewDidLoad {
    UISwipeGestureRecognizer *recognizer;

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)];
    [[self view] addGestureRecognizer:recognizer];
    [recognizer release]; 
    [super viewDidLoad];
}

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"Swipe received.");
}

뷰에 4개의 인식기를 추가하여 수정했는데 문서에 광고된 대로 작동하지 않는 이유가 궁금합니다.

- (void)viewDidLoad {
    UISwipeGestureRecognizer *recognizer;

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [[self view] addGestureRecognizer:recognizer];
    [recognizer release];

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [[self view] addGestureRecognizer:recognizer];
    [recognizer release];

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [[self view] addGestureRecognizer:recognizer];
    [recognizer release];

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [[self view] addGestureRecognizer:recognizer];
    [recognizer release];

    [super viewDidLoad];
}

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"Swipe received.");
}

벌레가 있는 것 같습니다.허용된 방향을 지정할 수 있습니다.그러나 액션 선택기 방법에서 스와이프를 트리거한 실제 방향에 액세스하려고 하면 허용된 방향에 대해 원래 설정한 비트 마스크가 계속 표시됩니다.

즉, 두 개 이상의 방향이 허용될 경우 실제 방향에 대한 검사는 항상 실패합니다.셀렉터 방식에서 '방향' 값을 출력하면 매우 쉽게 확인할 수 있습니다(즉,-(void)scrollViewSwiped:(UISwipeGestureRecognizer *)recognizer).

Apple에 버그 보고서(#8276386)를 제출했습니다.

[Update] Apple에서 의도한 대로 동작한다는 답변을 받았습니다.

예를 들어 테이블 보기에서 테이블 보기 셀에서 왼쪽 또는 오른쪽으로 스와이프하여 '삭제'를 트리거할 수 있습니다(스와이프 제스처의 방향이 왼쪽 및 오른쪽으로 설정됨).

이는 원래 해결 방법이 사용해야 하는 방식임을 의미합니다.방향 속성은 제스처를 올바르게 인식하는 데만 사용할 수 있지만 인식을 트리거한 실제 방향과 비교하기 위해 성공적인 인식에서 수행되는 방법에서는 사용할 수 없습니다.

왼쪽/오른쪽 제스처와 위쪽/아래쪽 제스처가 쌍으로 함께 작동하므로 두 개의 제스처 인식기만 지정하면 됩니다.그리고 그 문서들은 잘못된 것 같습니다.

정말 안됐군요, 라스가 언급한 것처럼 두 가지 제스처를 추가하여 문제를 해결했고 완벽하게 작동했습니다.

왼쪽/오른쪽 2) 위/아래

  

UISwipeGestureRecognizer *swipeLeftRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    [swipeLeftRight setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft )];
    [self.view addGestureRecognizer:swipeLeftRight];

    UISwipeGestureRecognizer *swipeUpDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    [swipeUpDown setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown )];
    [self.view addGestureRecognizer:swipeUpDown];
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];

이제 이것이 didSwip 기능입니다.

- (void) didSwipe:(UISwipeGestureRecognizer *)recognizer{
      if([recognizer direction] == UISwipeGestureRecognizerDirectionLeft){
          //Swipe from right to left
          //Do your functions here
      }else{
          //Swipe from left to right
          //Do your functions here
      }
 }

Xcode 4.2를 사용하는 경우 스토리보드에서 제스처 인식자를 추가한 다음 GUI 제스처 인식자를 IBActions에 연결할 수 있습니다.

제스처 인식자는 유틸리티 창의 개체 라이브러리(오른쪽 창의 맨 아래)에서 찾을 수 있습니다.

그러면 적절한 조치로 제어를 끌고 가는 것이 문제일 뿐입니다.

네 방향을 모두 감지하려면 해결 방법에서와 같이 네 개의 인스턴스를 만들어야 합니다.

이유:생성하는 UISwipGestureRecognizer와 동일한 인스턴스는 선택기에 보낸 사람으로 전달되는 인스턴스입니다.따라서 네 방향을 모두 인식하도록 설정하면 다음 시간 동안 true로 반환됩니다.sgr.direction == xxx여기서 xxx는 네 방향 중 하나입니다.

다음은 코드를 적게 사용하는 대안적인 해결 방법입니다(ARC 사용을 가정).

for(int d = UISwipeGestureRecognizerDirectionRight; d <= UISwipeGestureRecognizerDirectionDown; d = d*2) {
    UISwipeGestureRecognizer *sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    sgr.direction = d;
    [self.view addGestureRecognizer:sgr];
}

스위프트 2.1

나는 다음을 사용해야 했습니다.

    for var x in [
        UISwipeGestureRecognizerDirection.Left,
        UISwipeGestureRecognizerDirection.Right,
        UISwipeGestureRecognizerDirection.Up,
        UISwipeGestureRecognizerDirection.Down
    ] {
        let r = UISwipeGestureRecognizer(target: self, action: "swipe:")
        r.direction = x
        self.view.addGestureRecognizer(r)
    }

다음은 UISwipGestureRecognizer 사용에 대한 코드 샘플입니다.주석을 기록합니다.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //add gesture recognizer. The 'direction' property of UISwipeGestureRecognizer only sets the allowable directions. It does not return to the user the direction that was actaully swiped. Must set up separate gesture recognizers to handle the specific directions for which I want an outcome.
    UISwipeGestureRecognizer *gestureRight;
    UISwipeGestureRecognizer *gestureLeft;
    gestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];//direction is set by default.
    gestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];//need to set direction.
    [gestureLeft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    //[gesture setNumberOfTouchesRequired:1];//default is 1
    [[self view] addGestureRecognizer:gestureRight];//this gets things rolling.
    [[self view] addGestureRecognizer:gestureLeft];//this gets things rolling.
}

오른쪽 스와이프 및 왼쪽 스와이프는 왼쪽 또는 오른쪽 스와이프를 기반으로 특정 작업을 수행하는 데 사용하는 방법입니다.예:

- (void)swipeRight:(UISwipeGestureRecognizer *)gesture
{
    NSLog(@"Right Swipe received.");//Lets you know this method was called by gesture recognizer.
    NSLog(@"Direction is: %i", gesture.direction);//Lets you know the numeric value of the gesture direction for confirmation (1=right).
    //only interested in gesture if gesture state == changed or ended (From Paul Hegarty @ standford U
    if ((gesture.state == UIGestureRecognizerStateChanged) ||
    (gesture.state == UIGestureRecognizerStateEnded)) {

    //do something for a right swipe gesture.
    }
}
UISwipeGestureRecognizer *Updown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleGestureNext:)];
            Updown.delegate=self;
            [Updown setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp];
            [overLayView addGestureRecognizer:Updown];

            UISwipeGestureRecognizer *LeftRight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleGestureNext:)];
            LeftRight.delegate=self;
            [LeftRight setDirection:UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight];
            [overLayView addGestureRecognizer:LeftRight];
            overLayView.userInteractionEnabled=NO;


    -(void)handleGestureNext:(UISwipeGestureRecognizer *)recognizer
    {
        NSLog(@"Swipe Recevied");
        //Left
        //Right
        //Top
        //Bottom
    }

음, 이상해요, 저한테 딱 맞아요, 저도 똑같은 일을 해요.

당신이 한번 봐야 한다고 생각합니다.

IMT2000 3GPP - UIGesture RecognizerDelegate 메서드

- (BOOL)gestureRecognizerShouldBegin:(UISwipeGestureRecognizer *)gestureRecognizer {
   // also try to look what's wrong with gesture
   NSLog(@"should began gesture %@", gestureRecognizer);
   return YES;
}

로그에는 다음과 같은 것이 표시되어야 합니다.

제스처를 시작해야 합니다; target= <(action=actionForUpDownSwipGestureRecognizer:, target=)>; 방향 = 위, 아래, 왼쪽, 오른쪽>

이를 사용합니다. 비트 작업이어야 합니다.

   gesture.direction & UISwipeGestureRecognizerDirectionUp || 
   gesture.direction & UISwipeGestureRecognizerDirectionDown

이건 날 미치게 했어요.마침내 여러 개의 스와이프 제스처 인식기를 사용할 수 있는 믿을 수 있는 방법을 찾았습니다.

여러 스와이프 제스처 인식기에서 "액션" 선택기의 이름이 같을 경우 iOS에 버그가 있는 것으로 나타납니다.이름만 다르게 지정하면 예를 들어 LeftSwipFrom 및 RightSwipFrom을 처리하면 모든 것이 작동합니다.

UISwipeGestureRecognizer *recognizer;

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];

언급URL : https://stackoverflow.com/questions/3319209/setting-direction-for-uiswipegesturerecognizer