【发布时间】:2019-08-28 17:18:15
【问题描述】:
我正在构建一个类似于 TikTok 的应用程序,其中主要提要是一个 UICollectionView,每个单元格中都有一个视频。我的问题是多个视频将同时播放,我只希望一次播放一个视频,具体取决于单元格是否在视图中。如果单元格不在视野范围内,我还希望视频暂停。
每个单元格占据整个屏幕,并且 collectionView 启用了分页。
如果您曾经使用过 Instagram 或 TikTok,我希望功能相同。视频仅在单元格进入视图时播放,视频在单元格离开视图时暂停。如果您需要更多代码或信息,请告诉我,谢谢!
这里是FeedController中的相关代码:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! FeedCell
cell.delegate = self
cell.post = posts[indexPath.row]
if let videoUrl = posts[indexPath.row].videoUrl {
player = AVPlayer(url: videoUrl)
playerLayer = AVPlayerLayer(player: player)
playerLayer?.zPosition = -1
playerLayer?.frame = cell.contentView.frame
playerLayer?.videoGravity = .resizeAspectFill
cell.contentView.layer.addSublayer(playerLayer!)
player?.play()
cell.playerLayer = playerLayer
cell.player = player
}
return cell
}
FeedCell的相关代码如下:
class FeedCell: UICollectionViewCell {
var post: Post?
var playerLayer: AVPlayerLayer?
var player: AVPlayer?
override func prepareForReuse() {
super.prepareForReuse()
playerLayer?.removeFromSuperlayer()
player?.pause()
}
}
【问题讨论】:
标签: ios swift collectionview