|
@@ -16,11 +16,37 @@ class CommunityVideoListController: BaseViewController {
|
|
|
|
|
|
var videoItemList: Array<CommunityVideoItemModel>?
|
|
|
|
|
|
+ private var prePlayCell: CommunityVideoCoverCollectionCell?
|
|
|
+
|
|
|
+ deinit {
|
|
|
+ if observe != nil {
|
|
|
+ NotificationCenter.default.removeObserver(observe!)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ weak var observe : NSObjectProtocol?
|
|
|
+
|
|
|
override func viewDidLoad() {
|
|
|
super.viewDidLoad()
|
|
|
setupViews()
|
|
|
setupData()
|
|
|
+ setUpAppStatusNotification()
|
|
|
+ }
|
|
|
+
|
|
|
+ func setUpAppStatusNotification() {
|
|
|
+ observe = NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: OperationQueue.main, using: {
|
|
|
+ [weak self] (notification) in
|
|
|
+ self?.pauseCurrentVideo()
|
|
|
+ })
|
|
|
|
|
|
+ observe = NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: OperationQueue.main, using: {
|
|
|
+ [weak self] (notification) in
|
|
|
+ self?.startPlay(self?.prePlayCell)
|
|
|
+ })
|
|
|
+
|
|
|
+ observe = NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: OperationQueue.main, using: {
|
|
|
+ [weak self] (notification) in
|
|
|
+ self?.startPlay(self?.prePlayCell)
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
override func setupViews() {
|
|
@@ -106,4 +132,34 @@ extension CommunityVideoListController: UICollectionViewDelegateFlowLayout,UICol
|
|
|
return CGSize(width: kScreenWidth, height: 10)
|
|
|
}
|
|
|
|
|
|
+ func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
|
|
|
+ prePlayCell = cell as? CommunityVideoCoverCollectionCell
|
|
|
+ self.pauseCurrentVideo()
|
|
|
+ }
|
|
|
+
|
|
|
+ func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
|
|
|
+ let index = Int(collectionView.contentOffset.y / CGFloat(kScreenHeight))
|
|
|
+ let indexPath = IndexPath(item: index, section: 0)
|
|
|
+ let cell = collectionView.cellForItem(at: indexPath) as? CommunityVideoCoverCollectionCell
|
|
|
+
|
|
|
+ if(cell != nil) {
|
|
|
+ self.startPlay(cell)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: -
|
|
|
+extension CommunityVideoListController {
|
|
|
+
|
|
|
+ func startPlay(_ cell: CommunityVideoCoverCollectionCell?) {
|
|
|
+ cell?.play()
|
|
|
+ }
|
|
|
+
|
|
|
+ func pauseCurrentVideo() {
|
|
|
+ if prePlayCell != nil {
|
|
|
+ prePlayCell?.pause()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|