在单元格中有视频时,我只想显示视频(在avPlayerViewController中),如果没有视频,则它会执行到详细视图的搜索
我已经尝试过了:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let post = postArray[indexPath.item]
let controller = PostDetailViewController.fromStoryboard(post: post)
self.navigationController?.pushViewController(controller, animated: true)
guard let videoURL = URL(string: post.videoLink) else {
return
}
let avPlayer = AVPlayer(url: videoURL)
let avController = AVPlayerViewController()
avController.player = avPlayer
present(avController, animated: true) {
avPlayer.play()
}
}
如果有一个视频(来自Firebase),它将播放视频,但也会打开详细视图。因此,我希望该单元仅打开视频或仅打开detailview
答案 0 :(得分:1)
对其进行结构化,以便在没有post.videoLink
的情况下按PostDetailViewController
,否则它将播放视频:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let videoURL = URL(string: post.videoLink) else {
//If there is no URL, then this else condition will be called. Otherwise, the code below the guard statement will be called, and the video player will appear.
let post = postArray[indexPath.item]
let controller = PostDetailViewController.fromStoryboard(post: post)
self.navigationController?.pushViewController(controller, animated: true)
return
}
let avPlayer = AVPlayer(url: videoURL)
let avController = AVPlayerViewController()
avController.player = avPlayer
present(avController, animated: true) {
avPlayer.play()
}
}