显示控件时仅在 AVPlayerViewController 中显示 contentOverlayView

Only Showing contentOverlayView in AVPlayerViewController when controls are showing

我正在尝试使用 contentOverlayView 向 AVPlayerViewController 添加一个额外的按钮。问题是我只想在播放器控件显示时显示按钮。我觉得在这个阶段这是不可能的,但想确定一下。有人可以确认目前这是否可行吗?

假设您 运行 AVPlayerViewController 全屏,您可以将 UITapGestureRecognizer 添加到控制器的视图中。但是需要一些 UIGestureRecognizerDelegate 方法。我将子视图添加到 AVPlayerViewController 的视图而不是 contentOverlayView。

let controller = AVPlayerViewController()
    controller.player = self.player
    let yourView = controller.view
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(VideoPlayer.onCustomTap(_:)))
    tapGestureRecognizer.numberOfTapsRequired = 1;
    tapGestureRecognizer.delegate = self
    yourView.addGestureRecognizer(tapGestureRecognizer)

    self.yourSubView = YourView()

    controller.view.addSubview(yourSubView)
    parentViewController.modalPresentationStyle = .FullScreen;
    parentViewController.presentViewController(controller, animated: false, completion: nil)

将 UIGestureRecognizerDelegate 添加到您的 class 并检查任何触摸视图的高度和宽度是否为屏幕高度和宽度。这个,虽然显然是 hacky 是我发现接近工作的唯一方法。播放器控件喜欢在播放 5 秒后消失并在结束时重新出现……这可能可以通过 AVPlayer 上的观察者来解决。我试过走这条路,像 ffwd 按钮之类的东西会让我放弃这个选择一个自定义播放器。

extension VideoPlayer: UIGestureRecognizerDelegate {

func onCustomTap(sender: UITapGestureRecognizer) {

    if yourSubView.alpha > 0{
        UIView.animateWithDuration(0.5, animations: {
            self.yourSubView.alpha = 0;
        })

    } else {
        UIView.animateWithDuration(0.5, animations: {
            self.yourSubView.alpha = 1;
        })
    }
}

public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {

    if let _touchView = touch.view {

        let screenRect:CGRect = UIScreen.mainScreen().bounds
        let screenWidth :CGFloat = screenRect.size.width;
        let screenHeight:CGFloat  = screenRect.size.height;

        if _touchView.bounds.height == screenHeight && _touchView.bounds.width == screenWidth{
            return true
        }

    }
    return false
}

public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}
}