5

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

4

1 回答 1

1

假设您正在全屏运行 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 添加到您的类并检查任何触摸视图的高度和宽度是否是屏幕高度和宽度。这虽然显然是 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
}
}
于 2016-06-07T16:06:01.180 回答