如何检查表视图单元格焦点和播放器设置暂停

Исм*_*тов 8 ios swift

在此输入图像描述

我的项目

我连续几张桌子上有几个视频.

问题是它们是同步播放的.在屏幕上1video,并2video播放同步

如何在桌面视图上跟踪焦点单元格cell.player?.play().和其他细胞cell.player?.pause()

我的代码:

   class MyViewController
//don't work
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) as? ListViewCell {
        cell.player?.pause()
    }
}


//don't call
func tableView(_ tableView: UITableView, didUpdateFocusIn context: UITableViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    print("table view cell didUpdateFocusIn")
}




class MyTableviewCell

override func prepareForReuse() {
        super.prepareForReuse()
        player?.pause()
    }
Run Code Online (Sandbox Code Playgroud)

小智 1

在你的 ViewController 中使用它

   extension ViewController {
        override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
            if keyPath == #keyPath(UITableView.contentOffset) {
                if let playIndexPath = currentPlayIndexPath {

                    if let cell = tblInstaFeed.cellForRow(at: playIndexPath) {
                        if player.displayView.isFullScreen { return }
                        let visibleCells = tblInstaFeed.visibleCells
                        if visibleCells.contains(cell) {
                      cell.contentView.addSubview(player.displayView)
                            player.displayView.snp.remakeConstraints{
                                $0.edges.equalTo(cell)
                            }
                            self.player.play()
                        } else {
                           player.displayView.removeFromSuperview()
                            self.player.pause()

                        }
                    }
                }
            }
          }
        }
Run Code Online (Sandbox Code Playgroud)

并这样称呼它:

var tableViewContext = 0
    func addTableViewObservers() {
        let options = NSKeyValueObservingOptions([.new, .initial])
        tblInstaFeed?.addObserver(self, forKeyPath: #keyPath(UITableView.contentOffset), options: options, context: &tableViewContext)
    }
Run Code Online (Sandbox Code Playgroud)

并在viewDidLoad中调用addTableViewObservers函数

希望这会有所帮助。