AVPlayercontroller - 添加叠加子视图

时间:2016-04-16 17:07:38

标签: swift avplayer tvos avplayerviewcontroller

取决于PlaybackControl Visibity如何显示和隐藏contentOverlayView中添加的自定义子视图?

我想像Youtube TVOS应用那样做。

我尝试使用UIPressesEvent,但它没有给我确切的触摸事件。它给了我这些:

  override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
    for item in presses {
        switch item.type {
        case .Menu:
            self.customViews.alpha = 0
        case .PlayPause:
            self.player?.pause()
            self.customViews.alpha = 0
        default:
            self.setVisibilityToPreviewView()
        }
      }
    }

 func setVisibilityToPreviewView () { //This wont work in all cases.
      if self.previewView.alpha == 1 {
         self.previewView.alpha = 0
      } else {
         self.previewView.alpha = 1
      }
    }

但是通过这个Touch事件,我只能显示和隐藏子视图。 当播放控件 隐藏时,它应该被隐藏。

如果我获得 PlayBackControl可见性值,我不需要担心隐藏这些子视图。

Apple正在使用 AVNowPlayingPlaybackControlsViewController 。它不适合开发人员。

所以我需要找到其他更好的方法来做到这一点。

请指导我怎么做。

1 个答案:

答案 0 :(得分:4)

您可以注册tapGesture识别器,然后将其allowPressTypes属性设置为UIPressType.Select,类似这样

let tapRecognizer = UITapGestureRecognizer(target: self, action: "onSelect:")
tapRecognizer.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)];
self.view.addGestureRecognizer(tapRecognizer)

在操作按钮内显示或隐藏自定义叠加层。

示例:在视图控制器中添加此代码并点击(在远程,触摸区域前面的空白区域选择),您将在控制台上看到一条消息。

override func viewDidLoad() {
        super.viewDidLoad()
        let tapGesture = UITapGestureRecognizer(target: self, action: "onSelect")
        tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
        self.view.addGestureRecognizer(tapGesture);
    }
    func onSelect(){
        print("this is select gesture handler method");
    }

更新:以下是创建AVPlayerController的代码,并将tapGestureRecognizer注册到playervc.view。

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        playContent()
    }
    func playContent(){
        let urlString = "<contentURL>"
        guard let url = NSURL(string: urlString) else{
            return
        }
        let avPlayer = AVPlayer(URL: url)
        let playerVC = AVPlayerViewController()
        playerVC.player = avPlayer
        self.playerObj = playerVC

        let tapGesture = UITapGestureRecognizer(target: self, action: "onSelect")
        tapGesture.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
        self.view.addGestureRecognizer(tapGesture);

        playerVC.view.addGestureRecognizer(tapGesture)
        self.presentViewController(playerVC, animated: true, completion: nil);

    }

尝试一下,我认为它应该可以正常工作。