我有以下UIGestureRecognizer可正确检测向上滑动

- (void) addGestureRecognizer {
    _swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
    [_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionUp];
    [_swipeRecognizer setDelegate:self];
    [self.view addGestureRecognizer:_swipeRecognizer];
}

- (void) didSwipe:(id)sender{
    NSLog(@"didSwipe");
}

然后,我修改了方向以包括左右
[_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight];

并且不再响应向上滑动(向左或向右滑动)。我究竟做错了什么? (在模拟器,iphone5,ipad3上尝试过)

注意:我不需要检测滑动的实际方向。我只需要知道有滑动即可。谢谢。

最佳答案

试试这个

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

    _swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
    [_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown];
    [_swipeRecognizer setDelegate:self];
    [self.view addGestureRecognizer:_swipeRecognizer];

编辑

as @ LearnCocos2D suggested“显然,每个UISwipeGestureRecognizer只能检测给定方向上的滑动。即使可以将方向标志一起或在一起,UISwipeGestureRecognizer也会忽略其他标志。”

并且按照您的“注意:我不需要检测滑动的实际方向。我只需要知道有滑动即可。”,您只需要检测滑动而不是方向,因此将左右组合为一个和其他手势一样可以上下移动。

关于ios - UISwipeGestureRecognizer不响应向上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15515248/

10-12 14:38