我有自定义视图:
class MediaPlayerView: UIView {
var mediaURL: URL? {
didSet {
determineMediaType()
}
}
let videoExtensions = ["mov"]
override init (frame : CGRect) {
super.init(frame : frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func determineMediaType() {
let url = self.mediaURL
let pathExtention = url?.pathExtension
if videoExtensions.contains(pathExtention!) {
print("Movie URL: \(String(describing: url))")
setupVideo(url: url!)
} else {
print("Image URL: \(String(describing: url))")
setupImage(url: url!)
}
}
func setupVideo(url: URL) {
let playButton = UIImage(named: "Play Triangle")
let playButtonView = UIImageView(image: playButton!)
let singleTap = UITapGestureRecognizer(target: self, action: #selector(tapDetected))
playButtonView.addGestureRecognizer(singleTap)
playButtonView.center = self.center
playButtonView.frame.size.width = self.frame.size.width/5
playButtonView.frame.size.height = self.frame.size.height/5
playButtonView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
playButtonView.isUserInteractionEnabled = true
self.addSubview(playButtonView)
}
@objc func tapDetected() {
print("tap!")
let player = AVPlayer(url: self.mediaURL!)
let controller = AVPlayerViewController()
controller.player = player
self.window?.rootViewController?.present(controller, animated: true) {
player.play()
}
}
func setupImage(url: URL) {
let imageView = UIImageView()
imageView.frame = self.bounds
imageView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
self.addSubview(imageView)
imageView.kf.setImage(with: url)
}
}
然而,当我点击播放按钮时,我收到以下错误:
Warning: Attempt to present <AVPlayerViewController: 0x7fe8b200b000> on <SweatNet.MainTabBarController: 0x7fe8b4816400> whose view is not in the window hierarchy!
它出现在这一行:self.window?.rootViewController?.present。我想我调用rootViewController(似乎是SweatNet.MainTabBarController)让我感到困惑。我想要它来调用SweatNet.TagViewController。这是包含自定义MediaPlayerView的单元格,但我不明白如何获取对此的引用。
答案 0 :(得分:2)
试试这个:
class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}
将您的方法写为:
@objc func tapDetected() {
print("tap!")
let player = AVPlayer(url: self.mediaURL!)
let controller = AVPlayerViewController()
controller.player = player
topViewController().present(controller, animated: true) {
player.play()
}
}
这将返回层次结构中的顶级vc。希望它有所帮助!
答案 1 :(得分:0)
我会对这个答案的一些反馈感兴趣,因为我认为这是我用错误的方式思考iOS系统的一个例子。我最终意识到的是我试图将视图控制器代码放在自定义视图中。查看控制器代码,作为启动AVPlayerViewController的代码。相反,我应该把这个逻辑放在我的视图控制器中。
我所做的工作是使用collectionView委托方法didSelectItemAt indexPath:IndexPath在视图控制器内部,其中集合视图的视图控制器逻辑如下所示:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == self.collectionView {
let post = posts[indexPath.row]
if post.isVideo == true {
self.performSegue(withIdentifier: "playVideo", sender: nil)
} else {
print("is image. might go full screen one day here")
}
}
这是有效的,我相信它比编写视图的自定义方面更好,以启动AVPlayerViewController。但也可能两种方法都有效 - 我真的对iOS开发知之甚少。欢迎任何评论。