以全屏Swift显示来自单元格的视频

Jaq*_*ine 3 video ios firebase swift

所以我想要做的是我有一个带有图像和视频单元格的 UICollectionViewController,我想要它,这样当你点击视频时,它会全屏播放视频。我已经为图像制作了这个,但是我不太确定如何为视频做到这一点?因此,对于图像,您点击图像(单元格),图像会以黑色背景视图显示在屏幕中央。我想合并同样的东西,但有视频,当然视频会播放等等......

我正在从 Firebase 获取视频。

关于我对图像的代码:

import UIKit
 import Firebase
 import MobileCoreServices
 import AVFoundation

   class ImagesAndVideosController: UICollectionViewController,  UICollectionViewDelegateFlowLayout {

lazy var messageImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.layer.cornerRadius = 16
    imageView.layer.masksToBounds = true
    imageView.contentMode = .scaleAspectFill
    imageView.isUserInteractionEnabled = true
    imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleZoomTap)))

    return imageView
}()



 func handleZoomTap(_ tapGesture: UITapGestureRecognizer) {
    if message?.videoUrl != nil {

        return
    }

    if let imageView = tapGesture.view as? UIImageView {

            self.chatLogController?.performZoomInForStartingImageView(imageView)
    }
}

 var startingFrame: CGRect?
 var blackBackgroundView: UIView?
 var startingImageView: UIImageView?

 func performZoomInForStartingImageView(_ startingImageView: UIImageView) {

    self.startingImageView = startingImageView
    self.startingImageView?.isHidden = true

    startingFrame = startingImageView.superview?.convert(startingImageView.frame, to: nil)

    let zoomingImageView = UIImageView(frame: startingFrame!)
    zoomingImageView.backgroundColor = UIColor.red
    zoomingImageView.image = startingImageView.image
    zoomingImageView.isUserInteractionEnabled = true
    zoomingImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleZoomOut)))

    if let keyWindow = UIApplication.shared.keyWindow {
        blackBackgroundView = UIView(frame: keyWindow.frame)
        blackBackgroundView?.backgroundColor = UIColor.black
        blackBackgroundView?.alpha = 0
        keyWindow.addSubview(blackBackgroundView!)

        keyWindow.addSubview(zoomingImageView)

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

            self.blackBackgroundView?.alpha = 1
            self.inputContainerView.alpha = 0

            // math?
            // h2 / w1 = h1 / w1
            // h2 = h1 / w1 * w1
            let height = self.startingFrame!.height / self.startingFrame!.width * keyWindow.frame.width

            zoomingImageView.frame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: height)

            zoomingImageView.center = keyWindow.center

        }, completion: { (completed) in
            //                    do nothing
        })

    }
}
 func handleZoomOut(_ tapGesture: UITapGestureRecognizer) {
    if let zoomOutImageView = tapGesture.view {
        //need to animate back out to controller
        zoomOutImageView.layer.cornerRadius = 16
        zoomOutImageView.clipsToBounds = true

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

            zoomOutImageView.frame = self.startingFrame!
            self.blackBackgroundView?.alpha = 0
            self.inputContainerView.alpha = 1

        }, completion: { (completed) in
            zoomOutImageView.removeFromSuperview()
            self.startingImageView?.isHidden = false
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

}

如果我提供的代码没有多大意义,我很抱歉,我相信你们都知道这些东西是非常机密的,所以我不得不进行足够的剪切和粘贴以显示但不是全部。所以我再次道歉。我也有一个播放按钮,所以当有一个视频加载到集合视图时,它知道并在它上面放一个播放按钮以便视频播放,但是视频在单元格内播放,这就是我想要的原因它转到中心(就像您在大多数应用程序中看到的那样)。

太感谢了!!我希望我提供了足够的信息?

Nex*_*hra 5

我已经创建了这种播放视频的方法。

    func playVideo() {
    guard let path = Bundle.main.path(forResource: "Video", ofType:"mp4") else {
        debugPrint("video.m4v not found")
        return
    }
    let player = AVPlayer(url: URL(fileURLWithPath: path))
    let playerController = AVPlayerViewController()
    playerController.player = player
    present(playerController, animated: true) {
        player.play()
    }
}
Run Code Online (Sandbox Code Playgroud)