无法正确获取UISwipeGestureRecognizer方向

时间:2013-04-24 06:07:55

标签: ios uigesturerecognizer uiswipegesturerecognizer

我正在编写代码,在视图上向上或向下移动我的两个手指以更改某些状态。代码如下:

UISwipeGestureRecognizer *aSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
[aSwipeGesture setDirection:UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown];
aSwipeGesture.numberOfTouchesRequired = 2;
[self.View addGestureRecognizer:aSwipeGesture];


- (void)swipeGesture:(UISwipeGestureRecognizer *)sender {
    NSLog(@"Swipe received.");
    if (sender.direction==UISwipeGestureRecognizerDirectionUp) {
        NSLog(@"swipe up");
    } else if (sender.direction==UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"swipe down");
    }
}

然而,我能收到的唯一打印日志是Swipe收到,如下所示。我无法收到刷卡或向下滑动的消息,我是否遗漏了什么?感谢

ViewController.m:228    Swipe received.
ViewController.m:228    Swipe received.
ViewController.m:228    Swipe received.
ViewController.m:228    Swipe received.
ViewController.m:228    Swipe received.
ViewController.m:228    Swipe received.

更新:我必须用两根手指才能完成滑动操作。

5 个答案:

答案 0 :(得分:6)

试试这个

UISwipeGestureRecognizer *aSwipeGestureUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
[aSwipeGesture setDirection:UISwipeGestureRecognizerDirectionUp];
aSwipeGestureUp.numberOfTouchesRequired = 2;
[self.View addGestureRecognizer:aSwipeGestureUp];

UISwipeGestureRecognizer *aSwipeGestureDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
[aSwipeGestureDown setDirection:UISwipeGestureRecognizerDirectionDown];
aSwipeGestureDown.numberOfTouchesRequired = 2;
[self.View addGestureRecognizer:aSwipeGestureDown];

答案 1 :(得分:5)

遗憾的是,API完全没有意义,你可以设置一个UISwipeGestureRecognizer来识别4个方向中任何一个方向的滑动,但它不会告诉你滑动的方向。

他们非常小心,允许4个常量一起使用(它是一个位图),但API刚刚破解。

你必须为每个方向使用一个识别器,这是一种耻辱!

答案 2 :(得分:2)

在您的情况下,您已将滑动方向设置为UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown。因此,在委托回调中,您将收到sender.direction的相同值,这就是因为枚举值与sender.direction值不匹配而不打印日志的原因。

如果您想单独处理向上和向下滑动,则需要使用向上和向下滑动方向创建2个滑动手势并添加到视图中。然后根据发件人的方向做任务。

答案 3 :(得分:1)

UISwipeGestureRecognizer * Updown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGestureNext :)];             Updown.delegate =自我;             [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
}

答案 4 :(得分:-3)

删除aSwipeGesture.numberOfTouchesRequired = 2;并尝试