Уничтожение прокручиваемых ячеек видео в UICollectionView для TikTok-подобной функциональности - PullRequest
0 голосов
/ 12 марта 2020

Пытаясь реализовать TikTok-подобную функциональность с полноэкранным просмотром видео, я относительно новичок в swift, поэтому, пожалуйста, простите меня за использование следующего проигрывателя https://github.com/piemonte/Player

С трудом уничтожая более ранние экземпляры ячеек в результате, когда приложение перемещается с фона на передний план, все видеоэкземпляры начинают воспроизводиться

Ниже следует мой контроллер представления

import Player
import UIKit

class HomeViewController: UIViewController,UICollectionViewDelegateFlowLayout, UICollectionViewDataSource  {


    lazy var data :[homeModel] =  [
        homeModel(charge: 231,videoUrl:"vineet_179_2",currency:"rupees",tname:"Abhinav Mahajan",backgroundImage: #imageLiteral(resourceName: "3"),mDesc:"content,inthenight creator,Insad"),

        homeModel(charge: 2331,videoUrl:"vineet_179_2",currency:"rupees",tname:"Kuwber Singh",backgroundImage: #imageLiteral(resourceName: "3"),mDesc:"Footballer"),

        homeModel(charge: 231,videoUrl:"vineet_179_2",currency:"rupees",tname:"Harsh Singh",backgroundImage: #imageLiteral(resourceName: "3"),mDesc:"Footballer,Koolboi"),

        homeModel(charge: 11,videoUrl:"vineet_179_2",currency:"rupees",tname:"Sameeh Hola",backgroundImage: #imageLiteral(resourceName: "3"),mDesc:"Singer,Footballer"),

        homeModel(charge: 43231,videoUrl:"vineet_179_2",currency:"rupees",tname:"Baag Singh",backgroundImage: #imageLiteral(resourceName: "3"),mDesc:"Cricketer"),
    ]

     let collectionView:UICollectionView = {

        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 0

        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)

        cv.translatesAutoresizingMaskIntoConstraints = false
        cv.register(CustomCell.self, forCellWithReuseIdentifier: "cell")

        return cv
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.reloadData()
        self.title = "HomeView"          
        view.addSubview(collectionView)
        collectionView.backgroundColor = .red
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsVerticalScrollIndicator = false //
        collectionView.isPagingEnabled = true
        print("--colelction view --")


        if #available(iOS 11.0, *) {
            collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
            collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
            collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
            collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
        } else {
            collectionView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
            collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
            collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
            collectionView.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor).isActive = true //fix
        }



    }


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            print("\(indexPath.row)")

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
            cell.configure(with: data[indexPath.row])
            print("---cellForItemAt--")
            return cell
    }


    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

        print("willDisplay")
    }


    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        (cell as? CustomCell)?.player.pause()
        self.collectionView(collectionView, willDisplay: cell, forItemAt: indexPath)
    }


    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

        collectionView.visibleCells.forEach { cell in

            (cell as? CustomCell)?.player.playFromBeginning()
            print("scrollViewDidEndDecelerating")
        }
    } 
}

Ниже указана пользовательская ячейка

import UIKit
import Player


class CustomCell: UICollectionViewCell {


    let indianCurrency = "₹"
    var localUrl = Bundle.main.url(forResource:"", withExtension: "")


    public func configure(with model: homeModel) {
        self.localUrl = Bundle.main.url(forResource:"\(model.videoUrl)", withExtension: "mp4")
        self.player.url = localUrl
        self.button.setTitle("Book now for \(indianCurrency)\(model.charge)", for: .normal)
        self.nameLabel.text = ("\(model.tname)")

        self.marqueeLabel.text = ("\(model.mDesc)")
        print("cell configure called")
    }




    override func prepareForReuse() {
        super.prepareForReuse()
        self.localUrl = Bundle.main.url(forResource:"", withExtension: "MOV")
    }


    let player: Player = {
        let homePlay = Player()
        homePlay.playerView.playerBackgroundColor = .cyan
        homePlay.playerView.translatesAutoresizingMaskIntoConstraints = false
        homePlay.playbackLoops = true
        homePlay.url = Bundle.main.url(forResource: "re", withExtension: "mp4")
        return homePlay
    }()

    override init(frame: CGRect) {
        super.init(frame: .zero)
        setupView()

        setupPlayerView()
    }

    func setupView(){

        contentView.backgroundColor = .green
        contentView.addSubview(homeView)

        homeView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
        homeView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
        homeView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
        homeView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
        homeView.backgroundColor = .green

    }

    func setupPlayerView(){
        homeView.addSubview(self.player.playerView)
        player.playerView.isHidden = false
        player.playerView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
        player.playerView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
        player.playerView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
        player.playerView.heightAnchor.constraint(equalTo: contentView.heightAnchor).isActive = true
        player.playerView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
        let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGestureRecognizer(_:)))
        tapGestureRecognizer.numberOfTapsRequired = 1
        player.view.addGestureRecognizer(tapGestureRecognizer)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension CustomCell {

    @objc func handleTapGestureRecognizer(_ gestureRecognizer: UITapGestureRecognizer) {
        switch self.player.playbackState {
        case .stopped:
            self.player.playFromBeginning()
            break
        case .paused:
            self.player.playFromCurrentTime()
            break
        case .playing:
            self.player.pause()
            break
        case .failed:
            self.player.pause()
            break
        }
    }
}

Кроме того, это модель, которую я использую для локального воспроизведения видео

struct homeModel {
    var charge:Int
    var videoUrl:String
    var currency:String
    var tname:String
    var backgroundImage: UIImage
    var mDesc:String
}

struct Video {

    let url: URL?

    init(url: URL?) {
        self.url = url
    }
}

Может кто-нибудь сказать, что мне не хватает?

...