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

时间:2015-08-07 11:32:08

标签: ios avplayerviewcontroller

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

1 个答案:

答案 0 :(得分: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
}
}