我已經搜索到處找到解決方案。通過寫這個問題再次嘗試我的運氣。我有一個collectionViewCell,其中我已經解析了包含名稱和ID的XML URL數據。現在當我點擊單元格時,我想加載一個帶有AVPlayer的UIView。要播放該文件,我必須使用其他網址,但使用的是我在該單元格中解析過的ID。這裏是我的didSelectItem func
:無法加載UIView與UICollectionVIewCell數據
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let playerLauncher = PlayerLauncher()
playerLauncher.showPlayer()
}
這是我PlayerLauncher文件:
import UIKit
import AVFoundation
class PlayerView: UIView {
var item: Item?
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.black
let id = item?.itemId
if let url = URL(string: "http://xample.com/play.m3u?id=\(id)") {
let player = AVPlayer(url: url)
print(url)
let playerLayer = AVPlayerLayer(player: player)
self.layer.addSublayer(playerLayer)
playerLayer.frame = self.frame
player.play()
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class PlayerLauncher: NSObject {
func showPlayer() {
if let keyWindow = UIApplication.shared.keyWindow {
let view = UIView(frame: keyWindow.frame)
view.backgroundColor = UIColor.white
view.frame = CGRect(x: 0, y: keyWindow.frame.height - 50, width: view.frame.width, height: 50)
let playerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.height)
let playerView = PlayerView(frame: playerFrame)
view.addSubview(playerView)
keyWindow.addSubview(view)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
view.frame = keyWindow.frame
}, completion: nil)
}
}
}
我從我的項目NSObject類,但選擇單元格中的UIView加載罰款後設定的項目,但不會玩文件並顯示id = nil
。我該如何解決它?
更新:所以我改了一下我的代碼,我可以打印id。
下面是我在didSelectItem
方法所做的更改:我在PlayerView和PlayerLauncher類改變了一些代碼
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath, animated: true)
let playerView = PlayerView()
playerView.item = items?[indexPath.item]
let playerLauncher = PlayerLauncher()
playerLauncher.showPlayer()
}
在此之後:
import UIKit
import AVFoundation
class PlayerView: UIView {
var id: String?
var item: Item? {
didSet {
id = item?.itemId
print(id)
}
}
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.black
}
func startPlaying() {
if let url = URL(string: "http://xample.com/play.m3u?id=\(id)") {
let player = AVPlayer(url: url)
print(url)
let playerLayer = AVPlayerLayer(player: player)
self.layer.addSublayer(playerLayer)
playerLayer.frame = self.frame
player.play()
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class PlayerLauncher: NSObject {
func showPlayer() {
if let keyWindow = UIApplication.shared.keyWindow {
let view = UIView(frame: keyWindow.frame)
view.backgroundColor = UIColor.white
view.frame = CGRect(x: 0, y: keyWindow.frame.height - 50, width: view.frame.width, height: 50)
let playerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.height)
let playerView = PlayerView(frame: playerFrame)
view.addSubview(playerView)
keyWindow.addSubview(view)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
view.frame = keyWindow.frame
}, completion: { (completeAnimation) in
playerView.startPlaying()
})
}
}
}
現在我可以從didSet
打印ID,但當在startPlaying()
方法中時,它仍然顯示id = nil
當我選擇該項目。我如何獲得在init
或startPlaying()
func中的didSet
中設置的屬性?
謝謝你的回答。你能告訴我如何創建一個實例嗎? – Mohit