|
@@ -13,6 +13,17 @@ import SnapKit
|
|
|
|
|
|
class BFVideoThumbProgressView: UIView {
|
|
|
var videoAsset : AVURLAsset?
|
|
|
+ var dragScrollProgressHandle : ((Float) -> Void)?
|
|
|
+ var dragEndHandle : ((Float) -> Void)?
|
|
|
+ var isDrag = false
|
|
|
+ var progress:Double = 0 {
|
|
|
+ didSet{
|
|
|
+ if let second = self.videoAsset?.duration.seconds, second > 0 {
|
|
|
+ let w = progressView.contentSize.width - width
|
|
|
+ progressView.contentOffset = CGPoint(x: progress * w / second, y: 0)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
var thumbImgs = [UIImage]()
|
|
|
|
|
@@ -20,7 +31,17 @@ class BFVideoThumbProgressView: UIView {
|
|
|
super.init(frame: frame)
|
|
|
self.videoAsset = videoAsset
|
|
|
addSubview(progressView)
|
|
|
-
|
|
|
+
|
|
|
+ let line = UIView()
|
|
|
+ line.backgroundColor = .white
|
|
|
+ line.layer.shadowColor = UIColor.black.cgColor
|
|
|
+ line.layer.shadowOffset = CGSize(width: 1, height: 1)
|
|
|
+ addSubview(line)
|
|
|
+ line.snp.makeConstraints { make in
|
|
|
+ make.width.equalTo(2)
|
|
|
+ make.center.height.equalToSuperview()
|
|
|
+ }
|
|
|
+
|
|
|
splitVideoFileUrlFps(urlAsset: videoAsset, fps: 2) {[weak self] isSuccess, images in
|
|
|
if isSuccess{
|
|
|
self?.thumbImgs = images!
|
|
@@ -29,6 +50,7 @@ class BFVideoThumbProgressView: UIView {
|
|
|
for (i, img) in images!.enumerated() {
|
|
|
let iv = UIImageView(image: img)
|
|
|
iv.contentMode = .scaleAspectFill
|
|
|
+ iv.clipsToBounds = true
|
|
|
sself.progressView.addSubview(iv)
|
|
|
iv.snp.makeConstraints { make in
|
|
|
make.left.equalTo(CGFloat(i) * sself.height + sself.width * 0.5)
|
|
@@ -53,17 +75,11 @@ class BFVideoThumbProgressView: UIView {
|
|
|
sv.backgroundColor = .clear
|
|
|
sv.decelerationRate = .fast
|
|
|
sv.showsHorizontalScrollIndicator = false
|
|
|
-
|
|
|
+ sv.delegate = self
|
|
|
|
|
|
return sv
|
|
|
}()
|
|
|
|
|
|
- override func didMoveToWindow() {
|
|
|
- super.didMoveToWindow()
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
override func layoutSubviews() {
|
|
|
super.layoutSubviews()
|
|
|
progressView.snp.makeConstraints { make in
|
|
@@ -72,3 +88,34 @@ class BFVideoThumbProgressView: UIView {
|
|
|
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+extension BFVideoThumbProgressView : UIScrollViewDelegate {
|
|
|
+ func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
|
|
+ if isDrag{
|
|
|
+ let dur = scrollView.contentOffset.x / (scrollView.contentSize.width - self.width)
|
|
|
+ self.dragScrollProgressHandle?(Float(dur))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
|
|
|
+ isDrag = true
|
|
|
+ let dur = scrollView.contentOffset.x / (scrollView.contentSize.width - self.width)
|
|
|
+ self.dragScrollProgressHandle?(Float(dur))
|
|
|
+ }
|
|
|
+
|
|
|
+ func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
|
|
|
+ if !decelerate {
|
|
|
+ let dur = scrollView.contentOffset.x / (scrollView.contentSize.width - self.width)
|
|
|
+ isDrag = false
|
|
|
+ dragEndHandle?(Float(dur))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
|
|
|
+ let dur = scrollView.contentOffset.x / (scrollView.contentSize.width - self.width)
|
|
|
+ isDrag = false
|
|
|
+ dragEndHandle?(Float(dur))
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|