我的 AVPlayerViewController
有这段代码。
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAvPlayer)];
[self.avPlayerViewController.view addGestureRecognizer:tap];
但这不起作用.. :S, 我试过设置
[self.avPlayerViewController.view setUserInteractionEnabled:YES];
还是不行..
唯一可行的解决方案是使用 UIGestureRecognizer
并实现它的 shouldReceiveTouch
委托(delegate)并检查 av 播放器是否被触摸..但问题是,我们不想捕获“点击释放”事件。因为如果 av player
View 被触摸,它会立即执行代码,这不是我们想要的......
请帮助我们解决这个问题..
谢谢!
最佳答案
AVPlayerViewController 上手势处理的正确位置是在 Controller 的 contentOverlayView 内。
contentOverlayView 是 AVPlayerViewController 的只读属性。这是一个显示在视频上方但在 Controller 下方的 View 。触摸处理的完美场所。您可以在加载时向其添加 subview 或手势处理程序。
以下代码以两种方式为您的视频 Controller 提供触摸支持,以演示手势识别和 UIButton 方法。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"mySegue"])
{
// Set things up when we're about to go the the video
AVPlayerViewController *avpvc = segue.destinationViewController;
AVPlayer *p = nil;
p = [AVPlayer playerWithURL:[NSURL URLWithString:@"https://my-video"]];
avpvc.player = p;
// Method 1: Add a gesture recognizer to the view
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myAction:)];
avpvc.contentOverlayView.gestureRecognizers = @[tap];
// Method 2: Add a button to the view
UIButton *cov = [UIButton buttonWithType:UIButtonTypeCustom];
[cov addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
cov.frame = self.view.bounds;
}
}
(抱歉,目前还没有准备好编写 Swift 版本。:)
关于ios - AVPlayer UITapGestureRecognizer 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36878609/