Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how check table view cell focus and player set pause

Tags:

ios

swift

enter image description here

my project

I have several videos in the table in a row.

The problem is that they are played synchronously. On screen 1video and 2video play synchronously

How do i can track focus cell on table view and cell.player?.play(). And other cells cell.player?.pause()

my code:

   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()
    }
like image 731
Kerim Khasbulatov Avatar asked Apr 28 '18 10:04

Kerim Khasbulatov


Video Answer


1 Answers

I'm making some changes to your project. Check this https://yadi.sk/d/bQ-eIyMz3VF28V

Decide which video to play or pause when scrolling:

func handleScroll() {
    if let indexPathsForVisibleRows = myTableView.indexPathsForVisibleRows, indexPathsForVisibleRows.count > 0 {
        var focusCell: ListViewCell?

        for indexPath in indexPathsForVisibleRows {
            if let cell = myTableView.cellForRow(at: indexPath) as? ListViewCell {
                if focusCell == nil {
                    let rect = myTableView.rectForRow(at: indexPath)
                    if myTableView.bounds.contains(rect) {
                        cell.player?.play()
                        focusCell = cell
                    } else {
                        cell.player?.pause()
                    }
                } else {
                    cell.player?.pause()
                }
            }
        }
    }
}

Hope this will help you.

like image 151
Bannings Avatar answered Nov 15 '22 22:11

Bannings