取决于播放控制可见性如何显示和隐藏添加到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
}
}
但有了这个触摸事件,我只能显示和隐藏子视图。当播放控制为隐藏时,应将其隐藏。
如果我得到PlayBackControl Visibility值,我就不必担心隐藏这些子视图。
Apple正在使用AVNowPlayingPlaybackControlsViewController。它不对开发人员开放。
所以我需要找到其他更好的方法来做到这一点。
请指导我如何做。
您可以注册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);
}
试试看,我认为它应该很好用。