AVPlayer UITapGestureRecognizer无法正常工作

最后发布: 2016-04-27 01:14:06


问题

我有我的AVPlayerViewController代码。

UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAvPlayer)];
[self.avPlayerViewController.view addGestureRecognizer:tap];

但这不起作用..:S,我尝试过设置

[self.avPlayerViewController.view setUserInteractionEnabled:YES];

仍然没有好......

唯一shouldReceiveTouch解决方案是使用UIGestureRecognizer并实现它的shouldReceiveTouch委托并检查是否触摸了av播放器..但问题是,我们不想捕获“tap release”事件..因为如果av player视图只是触摸,它立即执行代码,这不是我们想要的...

请帮我们解决这个问题..

谢谢!

ios objective-c iphone uitapgesturerecognizer avplayerviewcontroller
回答

AVPlayerViewController上手势处理的正确位置在控制器的contentOverlayView中

contentOverlayView是AVPlayerViewController的只读属性。 这是一个显示在视频上但在控制器下的视图。 只是触摸处理的理想场所。 您可以在加载时向其添加子视图或手势处理程序。

以下代码以两种方式为您的视频控制器提供触摸支持,以演示手势识别和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版本,对不起。:)


回答

这应该做到这一点。 将识别器添加到播放器的子视图中:

[videoPlayerViewController.view.subviews[0] addGestureRecognizer:tap]

这是一个快速修复复杂的用户界面和手势,看看contentOverlayView看到TyR的答案


回答

据我所知,你真正想要的是什么

UILongPressGestureRecognizer

因为,

UITapGestureRecognizer

如果您添加,则没有任何发布类型的事件

UILongPressGestureRecognizer

到你的

avPlayerViewController

然后进去

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  if ( [gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] ) {

  }
  return YES;
}

然后你可以设置手势的最小持续时间,以获得更好的感觉,

[holdGesture setMinimumPressDuration:0.01]

然后你可以实现这个方法,

- (void)holdAction:(UILongPressGestureRecognizer *)holdRecognizer

然后检查识别器的状态并执行所需的功能


回答

您可以在avplayerViewController上添加transparent视图,并可以在该视图上添加tapgesturerecognizer 希望这会有所帮助:)


回答

简单的解决方案是在添加电影播放器​​后在同一电影持有人视图上添加手势视图。 无需将手势识别器直接添加到电影播放器​​。

  [self.movieHolderView addSubview:self.moviePlayer.view];


  [self.movieHolderView addSubview:self.gestureView];
  [self.movieHolderView bringSubviewToFront:self.gestureView];

现在我们可以像这样在手势视图上添加我们的手势识别器

self.fullScreenTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandler:)];
self.fullScreenTapGesture.numberOfTapsRequired = 1;
[self.gestureView addGestureRecognizer:self.fullScreenTapGesture];


回答

对于OS 11和早期操作系统,应添加不同的手势识别器:

通用解决方案(Swift 4):

let tap = UITapGestureRecognizer(target: self, action: #selector(videoTap))
        if #available(iOS 11.0, *) {
            playerVC?.contentOverlayView?.gestureRecognizers = [tap]
        } else {
            playerVC?.view.subviews.first?.addGestureRecognizer(tap)
        }

@objc func videoTap(_ button: UIButton) {
        print("Tapped on video")
    }


回答

斯威夫特4

在AVPlayerViewController的视图中添加手势识别器:

func playVideo() {
    let playerController = AVPlayerViewController()
    playerController.player = AVPlayer(url: URL(fileURLWithPath: videoPath))
    playerController.showsPlaybackControls = false
    playerController.view.isUserInteractionEnabled = true

    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeUp.direction = UISwipeGestureRecognizerDirection.up
    playerController.view.addGestureRecognizer(swipeUp)

    present(playerController, animated: false) {
        playerController.player?.play()
    }
}

@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    print("swipe up")
}


回答

或者你可以继承AVPlayerViewController并使用folowwing方法

得到控制: -

1.touchBegan 2.touchEnd

确保将此标志设置为self.avPlayerContr.showsPlaybackControls = false

希望这对你有用。