如何使 UISwipeGestureRecognizer 在与其他手势识别器 tvOS 的视图中工作
how to make UISwipeGestureRecognizer work in a view with other gesture recognizers 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");
}
您必须将您的手势识别器设置为与其他手势识别器同时工作。请使用 UIGestureRecognizerDelegate 的方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}
我的 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");
}
您必须将您的手势识别器设置为与其他手势识别器同时工作。请使用 UIGestureRecognizerDelegate 的方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}