class VideoCollectionViewCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, AVPlayerViewControllerDelegate {
let videCellId = "CellId"
var player = AVPlayer()
let indexPath = IndexPath(item: 0, section: 0)
var stories: [String]? {
didSet {
collectionView.reloadData()
}
}
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 30
layout.scrollDirection = .horizontal
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.clear
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
func setup() {
//backgroundColor = .cyan
addSubview(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: self.topAnchor, constant: 15).isActive = true
collectionView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 15).isActive = true
collectionView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 5).isActive = true
collectionView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 5).isActive = true
collectionView.delegate = self
collectionView.dataSource = self
collectionView.allowsSelection = true
collectionView.register(InsideVideoCell.self, forCellWithReuseIdentifier: videCellId)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return stories?.count ?? 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: videCellId, for: indexPath) as! InsideVideoCell
if let storiesArray = stories?[indexPath.item] {
let url = URL(string: storiesArray)
let playerItem = AVPlayerItem(url: url!)
self.player = AVPlayer(playerItem: playerItem)
player.actionAtItemEnd = .none
let videoPlayerLayer = AVPlayerLayer(player: self.player)
videoPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
videoPlayerLayer.frame = cell.bounds
cell.videoPlayerController.player = self.player
cell.videoPlayerController.view.frame = videoPlayerLayer.frame
cell.videoPlayerController.view.clipsToBounds = true
cell.videoPlayerController.view.layer.cornerRadius = 15
cell.contentView.isUserInteractionEnabled = true
cell.videoPlayerController.view.isUserInteractionEnabled = true
cell.addSubview(cell.videoPlayerController.view)
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Выбрана ячейка: (\(indexPath.section), \(indexPath.item))")
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: frame.width - 174, height: frame.height - 18)
}
required init?(coder aDecoder: NSCoder) {
fatalError("Error VideoCell")
}
private class InsideVideoCell: UICollectionViewCell, AVPlayerViewControllerDelegate {
public var videoPlayerController = AVPlayerViewController()
func addControls() {
videoPlayerController.showsPlaybackControls = false
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
addControls()
}
func setup() {
videoPlayerController.delegate = self
}
required init?(coder aDecoder: NSCoder) {
fatalError("Error ImagesCell")
}
}
}
imageView
все отлично работаетcell.addSubview(videoPlayerController.view)
не работает. Если убрать suview c видео то все работает. cell.contentView.isUserInteractionEnabled = true
cell.videoPlayerController.view.isUserInteractionEnabled = true
tap.delegate = self
tap.cancelsTouchesInView = false
tap.isEnabled = true
cell.addGestureRecognizer(tap)
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.collectionView.selectItem(at: IndexPath(item: 0, section: 0), animated: true, scrollPosition: .bottom)
self.collectionView(self.collectionView, didSelectItemAt: IndexPath(item: 0, section: 0))
}
QoS
,Synchronisation & Mutex
,NSRecursiveLock & Mutex Recursive lock
,NSCondition, NSLocking, pthread_cond_t
,GCD, Concurrent queues, Serial queues, sync-async
,Operation & OperationQueue & OperationBlock