UICollectionView 将包含视频的提要。当用户观看视频时,我希望它能播放。使用我当前的设置,一旦将多个视频加载到 Collection View 中,它们就会同时播放(我想这取决于分页)。
How do I play videos inline in a UICollectionView?
UICollectionView 提要中的单元格将包含一个 UIView,它将容纳视频播放器。这是 UIView 的类 PlayerViewClass:
import Foundation
import UIKit
import AVKit
import AVFoundation
class PlayerViewClass: UIView {
override static var layerClass: AnyClass {
return AVPlayerLayer.self
}
var playerLayer: AVPlayerLayer {
return layer as! AVPlayerLayer
}
var player: AVPlayer? {
get {
return playerLayer.player
}
set {
playerLayer.player = newValue
}
}
}
FeedViewController 中 Feed 的 collectionView cellForItemAt indexPath 委托(delegate)方法如下:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
if let cell = cell as? MyCollectionViewCell {
...
//Configuring the cell
...
//Video player
let avPlayer = AVPlayer(url: post.fullURL)
//Setting cell's player
cell.playerView.playerLayer.player = avPlayer
//TODO: Change so this is only executed when the user is inline.
cell.playerView.player?.play()
}
return cell
}
单元格 MyCollectionViewCell 有一个链接到 playerView UIView 的 IBOutlet:
class MyCollectionViewCell {
@IBOutlet weak var playerView: PlayerViewClass!
override func awakeFromNib() {
super.awakeFromNib()
//Setup, not relevant
}
}
我找到了以下 GitHub repo ,它显示了我想要实现的功能;但是,我有点不确定如何使用下面的设置执行此操作。
非常感谢!
最佳答案
您需要知道哪些单元格是可见的。您可以通过 UICollectionView
API 的 visibleCells
属性“免费”获得它。这是 documentation .
此外,您还需要在 UICollectionView
滚动时进行一些簿记。在查看 UICollectionView
的文档时,您会发现它是 UIScrollView
的子类。 UIScrollView
带有委托(delegate)方法,使您能够跟踪它。这里有一个“诗人的物理学”方法,可以帮助您完成这项任务:
假设这是您的 View Controller :
class YourViewController: UIViewController {
let collectionView = UICollectionView()
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}
}
然后,您将在此处实现您的 UICollectionViewDelegate
和 UICollectionViewDataSource
方法:
extension YourViewController: UICollectionViewDelegate, UICollectionViewDataSource {
// Bare bones implementation
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// TODO: need to implement
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// TODO: need to implem,ent
return UICollectionViewCell()
}
}
最后,您将实现 UIScrollViewDelegate
检测滚动开始和滚动结束的方法。您可以在此处实现开始/停止视频的逻辑:
extension YourViewController: UIScrollViewDelegate {
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
collectionView.visibleCells.forEach { cell in
// TODO: write logic to stop the video before it begins scrolling
}
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
collectionView.visibleCells.forEach { cell in
// TODO: write logic to start the video after it ends scrolling
}
}
}
您会想弄清楚停止/启动动画的时间,看看什么看起来不错,所以请随意浏览各种 UIScrollViewDelegate
方法。
关于ios - 如何在 UICollectionView Swift 中内联播放视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57566288/