How to recognize swipe in all 4 directions?

前端 未结 8 455
后悔当初
后悔当初 2020-12-03 01:00

I need to recognize swipes in all directions (Up/Down/Left/Right). Not simultaneously, but I need to recognize them.

I tried:

  UISw         


        
相关标签:
8条回答
  • 2020-12-03 02:02

    You set the direction like this

      UISwipeGestureRecognizer *Swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
      Swipe.direction = (UISwipeGestureRecognizerDirectionLeft | 
                         UISwipeGestureRecognizerDirectionRight |
                         UISwipeGestureRecognizerDirectionDown | 
                         UISwipeGestureRecognizerDirectionUp);
    

    That's what the direction will be when you get the callback, so it is normal that all your tests fails. If you had

    - (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender {
      if ( sender.direction | UISwipeGestureRecognizerDirectionLeft )
        NSLog(@" *** SWIPE LEFT ***");
      if ( sender.direction | UISwipeGestureRecognizerDirectionRight )
        NSLog(@" *** SWIPE RIGHT ***");
      if ( sender.direction | UISwipeGestureRecognizerDirectionDown )
        NSLog(@" *** SWIPE DOWN ***");
      if ( sender.direction | UISwipeGestureRecognizerDirectionUp )
        NSLog(@" *** SWIPE UP ***");
    }
    

    The tests would succeed (but the would all succeed so you wouldn't get any information out of them). If you want to distinguish between swipes in different directions you will need separate gesture recognizers.


    EDIT

    As pointed out in the comments, see this answer. Apparently even this doesn't work. You should create swipe with only one direction to make your life easier.

    0 讨论(0)
  • 2020-12-03 02:02
    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");
    }
    
    0 讨论(0)
提交回复
热议问题