ios – 在滚动CollectionView期间不必要地打开YouTube播放器

ios – 在滚动CollectionView期间不必要地打开YouTube播放器,第1张

概述我正在聊天机器人,其中不同类型的响应来自服务器,我在聊天屏幕中使用UICollectionView单元格显示响应.根据服务器响应呈现不同类型的单元.当服务器响应播放视频时,我正在呈现包含youtube播放器的单元格.我正在使用 https://github.com/kieuquangloc147/YouTubePlayer-Swift.问题是当我滚动聊天屏幕(collectionView)时,yo 我正在聊天机器人,其中不同类型的响应来自服务器,我在聊天屏幕中使用UICollectionVIEw单元格显示响应.根据服务器响应呈现不同类型的单元.当服务器响应播放视频时,我正在呈现包含youtube播放器的单元格.我正在使用 https://github.com/kieuquangloc147/YouTubePlayer-Swift.问题是当我滚动聊天屏幕(collectionVIEw)时,youtube播放器一次又一次地打开.有时它会阻止所有UI元素并停止滚动.我尝试了不同的方法,但无法解决它.这是代码:
PlayerVIEw:
import UIKitclass PlayerVIEw: UIVIEw,YouTubePlayerDelegate {    overrIDe init(frame: CGRect) {        super.init(frame: frame)        addYotubePlayer()    }    required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }    // youtube player    lazy var youtubePlayer: YouTubePlayerVIEw = {        let vIEwFrame = UIScreen.main.bounds        let player = YouTubePlayerVIEw(frame: CGRect(x: 0,y: 0,wIDth: vIEwFrame.wIDth - 16,height: vIEwFrame.height * 1/3))        player.delegate = self        return player    }()    // used as an overlay to dismiss the youtube player    let blackVIEw = UIVIEw()    // youtube player loader    lazy var playerIndicator: UIActivityIndicatorVIEw = {        let indicator = UIActivityIndicatorVIEw()        indicator.activityIndicatorVIEwStyle = .whiteLarge        indicator.hIDesWhenStopped = true        return indicator    }()    // shows youtube player    func addYotubePlayer() {        if let window = UIApplication.shared.keyWindow {            blackVIEw.frame = window.frame            self.addSubvIEw(blackVIEw)            blackVIEw.backgroundcolor = UIcolor(white: 0,Alpha: 0.5)            let tap = UITapGestureRecognizer(target: self,action: #selector(handledismiss))            tap.numberOfTapsrequired = 1            tap.cancelstouchesInVIEw = false            blackVIEw.addGestureRecognizer(tap)            let centerX = UIScreen.main.bounds.size.wIDth / 2            let centerY = UIScreen.main.bounds.size.height / 2            blackVIEw.addSubvIEw(playerIndicator)            playerIndicator.center = CGPoint(x: centerX,y: centerY)            playerIndicator.startAnimating()            blackVIEw.addSubvIEw(youtubePlayer)            youtubePlayer.center = CGPoint(x: centerX,y: centerY)            blackVIEw.Alpha = 0            youtubePlayer.Alpha = 0            UIVIEw.animate(withDuration: 0.5,delay: 0,usingSpringWithdamPing: 1,initialSpringVeLocity: 1,options: .curveEaSEOut,animations: {                self.blackVIEw.Alpha = 1                self.youtubePlayer.Alpha = 1            },completion: nil)        }    }    func play(_ vIDeoID: String) {        youtubePlayer.loadVIDeoID(vIDeoID)    }    @objc func handledismiss() {        blackVIEw.removeFromSupervIEw()        UIApplication.shared.keyWindow?.vIEwWithTag(24)?.removeFromSupervIEw()        UIApplication.shared.keyWindow?.removeFromSupervIEw()    }    func playerReady(_ vIDeoPlayer: YouTubePlayerVIEw) {        self.playerIndicator.stopAnimating()    }    func playerStateChanged(_ vIDeoPlayer: YouTubePlayerVIEw,playerState: YouTubePlayerState) {    }    func playerQualityChanged(_ vIDeoPlayer: YouTubePlayerVIEw,playbackQuality: YouTubePlaybackQuality) {    }}

YouTubePlayerCell(我在collectionVIEw中显示的母鸡服务器响应视频):

import UIKitclass YouTubePlayerCell: ChatMessageCell {    var player: PlayerVIEw = PlayerVIEw(frame: UIScreen.main.bounds)    overrIDe func setupVIEws() {        super.setupVIEws()        setupCell()    }    func setupCell() {        messageTextVIEw.frame = CGRect.zero        textBubbleVIEw.frame = CGRect.zero    }    func loadVIDeo(with vIDeoID: String) {        player.tag = 24        UIApplication.shared.keyWindow?.addSubvIEw(player)        player.play(vIDeoID)    }    overrIDe func prepareForReuse() {        super.prepareForReuse()        player.removeFromSupervIEw()        UIApplication.shared.keyWindow?.vIEwWithTag(24)?.removeFromSupervIEw()    }}

以下是我在UICollectionVIEw的cellForItemAt方法中呈现YouTubePlayerCell的方法

let message = messages[indexPath.row]    if message.actionType == ActionType.vIDeo_play.rawValue {                if let cell = collectionVIEw.dequeueReusableCell(withReuseIDentifIEr: ControllerConstants.youtubePlayerCell,for: indexPath) as? YouTubePlayerCell {                    self.resignResponders()                    if let vIDeoID = message.vIDeoData?.IDentifIEr {                        cell.loadVIDeo(with: vIDeoID)                    }                    return cell                }            }

完整的源代码可以在这里找到:https://github.com/imjog/susi_iOS/tree/ytplayer

解决方法 我可以在下面的代码中看到
if let vIDeoID = message.vIDeoData?.IDentifIEr {                        cell.loadVIDeo(with: vIDeoID)                    }

你正在调用loadVIDeo方法,它负责显示播放器.因此,在滚动时,您正在重用单元格,它会调用loadVIDeo方法并呈现播放器.所以解决方案是在显示单元格时默认不开始播放视频,在单元格视频覆盖上提供播放/暂停按钮,然后单击开始播放视频的按钮.如果我的分析错误,请告诉我,你有什么确切的问题.

总结

以上是内存溢出为你收集整理的ios – 在滚动CollectionView期间不必要地打开YouTube播放器全部内容,希望文章能够帮你解决ios – 在滚动CollectionView期间不必要地打开YouTube播放器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1103455.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存