How to recognize swipe in all 4 directions?

前端 未结 8 470
后悔当初
后悔当初 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 01:55

    Use a UIPanGestureRecogizer and detect the swipe directions you care about. see the UIPanGestureRecognizer documentation for details. -rrh

    // add pan recognizer to the view when initialized
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognized:)];
    [panRecognizer setDelegate:self];
    [self addGestureRecognizer:panRecognizer]; // add to the view you want to detect swipe on
    
    
    -(void)panRecognized:(UIPanGestureRecognizer *)sender
    {
        if (sender.state == UIGestureRecognizerStateBegan) {
            // you might want to do something at the start of the pan
        }
    
        CGPoint distance = [sender translationInView:self]; // get distance of pan/swipe in the view in which the gesture recognizer was added
        CGPoint velocity = [sender velocityInView:self]; // get velocity of pan/swipe in the view in which the gesture recognizer was added
        float usersSwipeSpeed = abs(velocity.x); // use this if you need to move an object at a speed that matches the users swipe speed
        NSLog(@"swipe speed:%f", usersSwipeSpeed);
        if (sender.state == UIGestureRecognizerStateEnded) {
            [sender cancelsTouchesInView]; // you may or may not need this - check documentation if unsure
            if (distance.x > 0) { // right
                NSLog(@"user swiped right");
            } else if (distance.x < 0) { //left
                NSLog(@"user swiped left");
            }
            if (distance.y > 0) { // down
                NSLog(@"user swiped down");
            } else if (distance.y < 0) { //up
                NSLog(@"user swiped up");
            }
            // Note: if you don't want both axis directions to be triggered (i.e. up and right) you can add a tolerence instead of checking the distance against 0 you could check for greater and less than 50 or 100, etc.
        }
    }
    

    CHANHE IN STATEEND CODE WIH THIS

    //if YOU WANT ONLY SINGLE SWIPE FROM UP,DOWN,LEFT AND RIGHT

    if (sender.state == UIGestureRecognizerStateEnded) {
            [sender cancelsTouchesInView]; // you may or may not need this - check documentation if unsure
            if (distance.x > 0 && abs(distance.x)>abs(distance.y)) { // right
                NSLog(@"user swiped right");
            } else if (distance.x < 0 && abs(distance.x)>abs(distance.y)) { //left
                NSLog(@"user swiped left");
            }
            if (distance.y > 0 && abs(distance.y)>abs(distance.x)) { // down
                NSLog(@"user swiped down");
            } else if (distance.y < 0 && abs(distance.y)>abs(distance.x)) { //up
                NSLog(@"user swiped up");
            }
    
        } 
    

提交回复
热议问题