-
Notifications
You must be signed in to change notification settings - Fork 205
/
VideoPhotoViewController.swift
81 lines (75 loc) · 3.07 KB
/
VideoPhotoViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// VideoPhotoViewController.swift
// Example
//
// Created by JiongXing on 2019/11/29.
// Copyright © 2019 JiongXing. All rights reserved.
//
import UIKit
import JXPhotoBrowser
import AVKit
class VideoPhotoViewController: BaseCollectionViewController {
override class func name() -> String { "视频与图片混合浏览" }
override class func remark() -> String { "微信我的相册浏览方式" }
override func makeDataSource() -> [ResourceModel] {
var result: [ResourceModel] = []
(0..<6).forEach { index in
let model = ResourceModel()
model.localName = index % 2 == 0 ? "video_\(index / 2)" : "local_\(index)"
result.append(model)
}
return result
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.jx.dequeueReusableCell(BaseCollectionViewCell.self, for: indexPath)
if indexPath.item % 2 == 0 {
cell.imageView.image = nil
cell.backgroundColor = .red
} else {
cell.imageView.image = self.dataSource[indexPath.item].localName.flatMap { UIImage(named: $0) }
cell.backgroundColor = .clear
}
return cell
}
override func openPhotoBrowser(with collectionView: UICollectionView, indexPath: IndexPath) {
let browser = JXPhotoBrowser()
browser.numberOfItems = {
self.dataSource.count
}
browser.cellClassAtIndex = { index in
index % 2 == 0 ? VideoCell.self : JXPhotoBrowserImageCell.self
}
browser.reloadCellAtIndex = { context in
JXPhotoBrowserLog.high("reload cell!")
let resourceName = self.dataSource[context.index].localName!
if context.index % 2 == 0 {
let browserCell = context.cell as? VideoCell
if let url = Bundle.main.url(forResource: resourceName, withExtension: "MP4") {
browserCell?.player.replaceCurrentItem(with: AVPlayerItem(url: url))
}
} else {
let browserCell = context.cell as? JXPhotoBrowserImageCell
browserCell?.imageView.image = UIImage(named: resourceName)
}
}
browser.cellWillAppear = { cell, index in
if index % 2 == 0 {
JXPhotoBrowserLog.high("开始播放")
(cell as? VideoCell)?.player.play()
}
}
browser.cellWillDisappear = { cell, index in
if index % 2 == 0 {
JXPhotoBrowserLog.high("暂停播放")
(cell as? VideoCell)?.player.pause()
}
}
browser.transitionAnimator = JXPhotoBrowserZoomAnimator(previousView: { index -> UIView? in
let path = IndexPath(item: index, section: indexPath.section)
let cell = collectionView.cellForItem(at: path) as? BaseCollectionViewCell
return cell?.imageView
})
browser.pageIndex = indexPath.item
browser.show()
}
}