2
所以我想要做的是我有一個UICollectionViewController與單元格的圖像和視頻,我希望它,所以當你點擊視頻它全屏播放視頻。我已經爲這些圖像製作了這個圖片,但是我不太清楚如何爲視頻執行此操作?因此,使用圖像時,點擊圖像(單元格),圖像將以黑色背景視圖進入屏幕中央。我想合併相同的東西,但與視頻,當然,視頻將播放等...從全屏顯示一個單元格的視頻Swift
我從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
})
}
}
}
我很抱歉,如果我提出沒有多大意義的代碼,我相信大家都知道,這東西是超級保密的,所以我不得不剪切和粘貼足以顯示,但不是全部。我再次道歉。 我也有一個播放按鈕,所以當有一個視頻加載到收藏視圖中時,它會知道並在其上面放置播放按鈕,以便播放視頻,但視頻播放在單元內部,這就是爲什麼我希望它能夠到達中心(就像你在大多數應用中看到的那樣)。
謝謝你這麼多!我希望我提供了足夠的信息?
非常感謝!看起來這是我正在尋找的!我明天會試一試並更新。 – Jaqueline
歡迎,我在我的項目中添加了相同的代碼,它工作正常。請更新我。 –
請不要忘記通過單擊項目 - >構建階段--->複製包資源並在此處添加視頻來複制您的視頻文件。 –