如何使UISwipeGestureRecognizer在其他手势识别器tvOS的视图中工作

时间:2016-12-15 22:45:14

标签: objective-c tvos

我的UITapGestureRecognizer手势按预期工作,但我一直在尝试将UISwipeGestureRecognizer添加到我的tvOS应用程序但是当我用模拟器测试它时它不起作用!

这是我的代码:

- (void)addScreenControlGesturesRecognizers {
    UITapGestureRecognizer *_oneTapMediaControl = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapMediaControl:)];
    _oneTapMediaControl.numberOfTapsRequired = 1;
    _oneTapMediaControl.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypeSelect]];

    [self.view addGestureRecognizer:_oneTapMediaControl];

    UITapGestureRecognizer *_doubleTapMediaControl = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTapControl:)];
    _doubleTapMediaControl.numberOfTapsRequired = 2;
    _doubleTapMediaControl.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypeSelect]];

    [self.view addGestureRecognizer:_doubleTapMediaControl];

    [_oneTapMediaControl requireGestureRecognizerToFail:_doubleTapMediaControl];

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

}

- (void)handleSwipeGestureRecognizer:(UISwipeGestureRecognizer *)recognizer {
        NSLog(@"Swipe Left"); 
}

1 个答案:

答案 0 :(得分:2)

您必须将手势识别器设置为与其他手势识别器同时工作。请使用UIGestureRecognizerDelegate的方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    return YES;
}