如何检测滑动手势方向?

时间:2012-01-11 01:28:55

标签: ios objective-c swipe gesture-recognition uiswipegesturerecognizer

我需要检测我的滑动手势的方向,我有问题。手势正在工作,但我不知道如何检测方向。     ......

swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(detectSwipe:)];
[swipeGesture setNumberOfTouchesRequired:1];
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp];
[appView addGestureRecognizer:swipeGesture];

-(void)detectSwipe:(UISwipeGestureRecognizer *)recognizer { 
switch (recognizer.direction) {
    case UISwipeGestureRecognizerDirectionUp:
        NSLog(@"smth1");
        break;


    case UISwipeGestureRecognizerDirectionDown:
        NSLog(@"smth2");
    default:
        break;
}
}

它不起作用:/

4 个答案:

答案 0 :(得分:49)

以下是我的一个项目的示例:

    // ...

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeLeft];

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(didSwipe:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeRight];

    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc]  initWithTarget:self action:@selector(didSwipe:)];
    swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    [self.view addGestureRecognizer:swipeUp];

    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:swipeDown];

    // ...

- (void)didSwipe:(UISwipeGestureRecognizer*)swipe{

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"Swipe Left");
    } else if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"Swipe Right");
    } else if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
        NSLog(@"Swipe Up");
    } else if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"Swipe Down");
    }
}

答案 1 :(得分:20)

direction属性仅定义被识别为滑动的允许的方向,而不是特定滑动的实际方向。

最简单的方法是使用两个独立的手势识别器。您还可以在手势开始时以及以locationInView:方法结束时检查触摸的位置。

答案 2 :(得分:11)

扩展omz的解决方案:

self.myView是我想要设置手势识别器的视图。下面的代码未经过测试,我想最好将识别器保持为property并将其添加到viewDidLoad()xib文件中。 selfUIViewController

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedLeft:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft ];
[self.view addGestureRecognizer:swipeLeft];

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedRight:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight ];
[self.view addGestureRecognizer:swipeRight];

将这两种方法添加到UIViewController并添加必要的操作:

- (IBAction)swipedRight:(UISwipeGestureRecognizer *)recognizer
{
    NSLog(@"swiped right");
}

- (IBAction)swipedLeft:(UISwipeGestureRecognizer *)recognizer
{
    NSLog(@"swiped left");
} 

答案 3 :(得分:0)

  

使用Swift 5滑动手势方向检测

    override func viewDidLoad() {
    super.viewDidLoad()

         let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
         let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))

         leftSwipe.direction = .left
         rightSwipe.direction = .right

         view.addGestureRecognizer(leftSwipe)
         view.addGestureRecognizer(rightSwipe)
    }

    @objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {

        if (sender.direction == .left) {
                print("Swipe Left")
        }

        if (sender.direction == .right) {
            print("Swipe Right")
        }
    }