|
@@ -1,351 +0,0 @@
|
|
|
-//
|
|
|
-// WSSVideoScrollView.swift
|
|
|
-// ScrollDemo
|
|
|
-//
|
|
|
-// Created by zhongbaojian on 2019/8/12.
|
|
|
-// Copyright © 2019 zhongbaojian. All rights reserved.
|
|
|
-//
|
|
|
-
|
|
|
-import UIKit
|
|
|
-
|
|
|
-class WSSVideoScrollView: UIView {
|
|
|
- // MARK: - Public
|
|
|
-
|
|
|
- var datas: [WSSCommonTemplateModel] = [] {
|
|
|
- didSet {
|
|
|
- reloadData()
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- var changePageHanle: ((_ current: Int, _ last: Int) -> Void)?
|
|
|
- var playActionHandle: ((WSSCommonTemplateModel?, Bool) -> Void)?
|
|
|
- var albumActionHandle: ((WSSCommonTemplateModel?) -> Void)?
|
|
|
- var shareActionHandle: ((WSSCommonTemplateModel?) -> Void)?
|
|
|
- var headerRefreshHandle: (() -> Void)?
|
|
|
- var footerRefreshHandle: (() -> Void)?
|
|
|
-
|
|
|
- override init(frame: CGRect) {
|
|
|
- super.init(frame: frame)
|
|
|
-
|
|
|
- bottomItem.isHidden = true
|
|
|
- bottomItem.y = -1.0 * frame.height
|
|
|
-
|
|
|
- addSubview(container)
|
|
|
- container.addSubview(topItem)
|
|
|
- container.addSubview(midItem)
|
|
|
- container.addSubview(bottomItem)
|
|
|
-
|
|
|
- itemViews.append(bottomItem)
|
|
|
- itemViews.append(topItem)
|
|
|
- itemViews.append(midItem)
|
|
|
- }
|
|
|
-
|
|
|
- required init?(coder aDecoder: NSCoder) {
|
|
|
- fatalError("init(coder:) has not been implemented")
|
|
|
- }
|
|
|
-
|
|
|
- func currentShowingItem() -> WSSRecommendCell {
|
|
|
- return itemViews[1]
|
|
|
- }
|
|
|
-
|
|
|
- func lastShowItem() -> WSSRecommendCell? {
|
|
|
- return lastItemView
|
|
|
- }
|
|
|
-
|
|
|
- func triggerHeaderRefresh() {
|
|
|
- container.mj_header.beginRefreshing()
|
|
|
- reset()
|
|
|
- }
|
|
|
-
|
|
|
- func endHeaderRefresh() {
|
|
|
- if container.mj_header.isRefreshing {
|
|
|
- container.mj_header.endRefreshing()
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- func endFooterRefresh(isNoMore: Bool) {
|
|
|
- if isNoMore {
|
|
|
- container.mj_footer.endRefreshingWithNoMoreData()
|
|
|
- } else {
|
|
|
- container.mj_footer.endRefreshing()
|
|
|
- container.mj_footer.resetNoMoreData()
|
|
|
- }
|
|
|
-
|
|
|
- container.contentInset = UIEdgeInsets.zero
|
|
|
-
|
|
|
- if let preModel = self.datas.safeObject(atIndex: self.currentIndex + 1), let last = self.itemViews.last {
|
|
|
- last.isHidden = false
|
|
|
- last.setup(withVideoModel: preModel)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- func reset() {
|
|
|
- currentIndex = 0
|
|
|
- lastIndex = 0
|
|
|
- lastItemView = nil
|
|
|
-
|
|
|
- bottomItem.isHidden = true
|
|
|
- bottomItem.y = -1.0 * frame.height
|
|
|
-
|
|
|
- topItem.y = 0.0
|
|
|
- midItem.y = container.height
|
|
|
-
|
|
|
- itemViews[0] = bottomItem
|
|
|
- itemViews[1] = topItem
|
|
|
- itemViews[2] = midItem
|
|
|
- }
|
|
|
-
|
|
|
- // MARK: - Private
|
|
|
-
|
|
|
- fileprivate var currentIndex: Int = 0
|
|
|
- fileprivate var lastIndex: Int = 0
|
|
|
- fileprivate var startDragPointY: CGFloat = 0.0
|
|
|
- fileprivate var itemViews: [WSSRecommendCell] = []
|
|
|
- fileprivate var lastItemView: WSSRecommendCell?
|
|
|
-
|
|
|
- fileprivate lazy var container: UIScrollView = {
|
|
|
- let scroller: UIScrollView = UIScrollView(frame: self.bounds)
|
|
|
- scroller.delegate = self
|
|
|
- scroller.contentSize = self.bounds.size
|
|
|
- scroller.isPagingEnabled = true
|
|
|
- scroller.showsVerticalScrollIndicator = false
|
|
|
- scroller.showsHorizontalScrollIndicator = false
|
|
|
- scroller.delaysContentTouches = false
|
|
|
-
|
|
|
- if #available(iOS 11.0, *) {
|
|
|
- scroller.contentInsetAdjustmentBehavior = .never
|
|
|
- } else {
|
|
|
- scroller.contentInset = UIEdgeInsets.zero
|
|
|
- }
|
|
|
- scroller.mj_header = WSSRefreshHeader(refreshingBlock: { [weak self] in
|
|
|
- self?.headerRefreshHandle?()
|
|
|
- })
|
|
|
- scroller.mj_footer = FVRefreshFooter(refreshingBlock: { [weak self] in
|
|
|
- self?.footerRefreshHandle?()
|
|
|
- })
|
|
|
-
|
|
|
- return scroller
|
|
|
- }()
|
|
|
-
|
|
|
- fileprivate lazy var topItem: WSSRecommendCell = {
|
|
|
- let cell = self.createRecommendCell()
|
|
|
- cell.y = 0.0
|
|
|
- cell.tag = 1001
|
|
|
- return cell
|
|
|
- }()
|
|
|
-
|
|
|
- fileprivate lazy var midItem: WSSRecommendCell = {
|
|
|
- let cell = self.createRecommendCell()
|
|
|
- cell.y = self.container.height
|
|
|
- cell.tag = 1002
|
|
|
- return cell
|
|
|
- }()
|
|
|
-
|
|
|
- fileprivate lazy var bottomItem: WSSRecommendCell = {
|
|
|
- let cell = self.createRecommendCell()
|
|
|
- cell.tag = 1003
|
|
|
- return cell
|
|
|
- }()
|
|
|
-
|
|
|
- private func reloadData() {
|
|
|
- let contentHeight: CGFloat = container.height * CGFloat(datas.count)
|
|
|
- container.contentSize = CGSize(width: container.width, height: contentHeight)
|
|
|
-
|
|
|
- WSSLog("🐱🐱🐱: 刷新一次: \(datas.count) +++ \(currentIndex) +++ \(itemViews.map({ ($0.tag, $0.isHidden) }))")
|
|
|
-
|
|
|
- for item in datas.enumerated() {
|
|
|
- let model = item.element
|
|
|
- if item.offset == currentIndex {
|
|
|
- let target = itemViews[1]
|
|
|
- target.isHidden = false
|
|
|
- target.setup(withVideoModel: model)
|
|
|
- } else if item.offset == currentIndex + 1 {
|
|
|
- let target = itemViews[2]
|
|
|
- target.isHidden = false
|
|
|
- target.setup(withVideoModel: model)
|
|
|
- } else if item.offset == currentIndex - 1 {
|
|
|
- let target = itemViews[0]
|
|
|
- target.isHidden = false
|
|
|
- target.setup(withVideoModel: model)
|
|
|
- } else {
|
|
|
- continue
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- fileprivate func changeSort(isUp: Bool) {
|
|
|
- if isUp {
|
|
|
- if let first = self.itemViews.first {
|
|
|
- first.y = first.y + 3 * container.height
|
|
|
-
|
|
|
- let mid = itemViews[1]
|
|
|
- let bottom = itemViews[2]
|
|
|
- itemViews[0] = mid
|
|
|
- itemViews[1] = bottom
|
|
|
- itemViews[2] = first
|
|
|
- }
|
|
|
- } else {
|
|
|
- if let last = self.itemViews.last {
|
|
|
- last.y = last.y - 3 * container.height
|
|
|
-
|
|
|
- let first = itemViews[0]
|
|
|
- let second = itemViews[1]
|
|
|
- itemViews[0] = last
|
|
|
- itemViews[1] = first
|
|
|
- itemViews[2] = second
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- fileprivate func createRecommendCell() -> WSSRecommendCell {
|
|
|
- let cell: WSSRecommendCell = WSSRecommendCell.ojs_loadFromNib()
|
|
|
- cell.frame = container.bounds
|
|
|
- cell.videoPlayButton.isSelected = false
|
|
|
- cell.playActionHandle = { [unowned self] model, flag in
|
|
|
- self.playActionHandle?(model, flag)
|
|
|
- }
|
|
|
- cell.albumActionHandle = { [unowned self] model in
|
|
|
- self.albumActionHandle?(model)
|
|
|
- }
|
|
|
- cell.shareActionHandle = { [unowned self] model in
|
|
|
- self.shareActionHandle?(model)
|
|
|
- }
|
|
|
- return cell
|
|
|
- }
|
|
|
-
|
|
|
- override func layoutSubviews() {
|
|
|
- super.layoutSubviews()
|
|
|
-
|
|
|
- if #available(iOS 11.0, *) {
|
|
|
- //
|
|
|
- } else {
|
|
|
- container.contentInset = UIEdgeInsets.zero
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- fileprivate func scrollItem(to index: Int, isChangePage: Bool) {
|
|
|
- WSSLog("🐱: 当前页 >>> \(index)")
|
|
|
-
|
|
|
- lastIndex = currentIndex
|
|
|
- lastItemView = itemViews[1]
|
|
|
- currentIndex = Int(index)
|
|
|
-
|
|
|
- let delta = currentIndex - lastIndex
|
|
|
- if delta < 0 {
|
|
|
- // 向下 -1
|
|
|
- let preIndex = currentIndex - 1
|
|
|
- if let model = self.datas.safeObject(atIndex: preIndex), let last = self.itemViews.last {
|
|
|
- last.isHidden = false
|
|
|
- last.setup(withVideoModel: model)
|
|
|
- last.currentIndexPath = IndexPath(row: preIndex, section: 0)
|
|
|
- changeSort(isUp: false)
|
|
|
-
|
|
|
-// WSSLog("🐱: 向下 >>> 000")
|
|
|
-
|
|
|
- } else {
|
|
|
- if currentIndex == 0 {
|
|
|
- // 第一项了,还是需要移动一下,但是隐藏
|
|
|
- if let last = self.itemViews.last {
|
|
|
- last.isHidden = true
|
|
|
- changeSort(isUp: false)
|
|
|
-
|
|
|
-// WSSLog("🐱: 向下 >>> 111")
|
|
|
- }
|
|
|
- } else {
|
|
|
-// WSSLog("🐱: 向下 >>> 222")
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- } else if delta > 0 {
|
|
|
- // 向上 +1
|
|
|
- let preIndex = currentIndex + 1
|
|
|
- if let model = self.datas.safeObject(atIndex: preIndex), let first = self.itemViews.first {
|
|
|
- first.isHidden = false
|
|
|
- first.setup(withVideoModel: model)
|
|
|
- first.currentIndexPath = IndexPath(row: preIndex, section: 0)
|
|
|
- changeSort(isUp: true)
|
|
|
-
|
|
|
-// WSSLog("🐱: 向上 >>> 000")
|
|
|
-
|
|
|
- } else {
|
|
|
- if currentIndex == datas.count - 1 {
|
|
|
- // 最后一项,还是需要移动一下,但是隐藏
|
|
|
- if let first = self.itemViews.first {
|
|
|
- first.isHidden = true
|
|
|
- changeSort(isUp: true)
|
|
|
-
|
|
|
-// WSSLog("🐱: 向上 >>> 111")
|
|
|
- }
|
|
|
- } else {
|
|
|
-// WSSLog("🐱: 向上 >>> 222")
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- } else {
|
|
|
- // 不变
|
|
|
-
|
|
|
-// WSSLog("🐱: 不变")
|
|
|
-
|
|
|
- if let currentModel = self.datas.safeObject(atIndex: currentIndex), let targetModel = self.currentShowingItem().templateModel {
|
|
|
-// WSSLog("🐱: 不变 》》》》 \(currentModel.templateId) ++++ \(targetModel.templateId)")
|
|
|
-
|
|
|
- if currentModel.templateId != targetModel.templateId {
|
|
|
- // 位置不对
|
|
|
- if let targetIndex = self.datas.firstIndex(where: { $0.templateId == targetModel.templateId }) {
|
|
|
- let tmpDelta = currentIndex - targetIndex
|
|
|
- changeSort(isUp: tmpDelta < 0)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if let lastModel = self.datas.safeObject(atIndex: self.lastIndex) {
|
|
|
- lastModel.selected = false
|
|
|
- }
|
|
|
- if let currentModel = self.datas.safeObject(atIndex: self.currentIndex) {
|
|
|
- currentModel.selected = true
|
|
|
- }
|
|
|
-
|
|
|
- if isChangePage {
|
|
|
- changePageHanle?(currentIndex, lastIndex)
|
|
|
- }
|
|
|
-
|
|
|
-// WSSLog("🐱🐱🐱: \(self.itemViews.map({ ($0.tag, $0.isHidden) }))")
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// MARK: -
|
|
|
-
|
|
|
-extension WSSVideoScrollView: UIScrollViewDelegate {
|
|
|
- func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
|
|
- //
|
|
|
- }
|
|
|
-
|
|
|
- func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
|
|
|
- startDragPointY = scrollView.contentOffset.y
|
|
|
- }
|
|
|
-
|
|
|
- func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
|
|
|
- //
|
|
|
- }
|
|
|
-
|
|
|
- func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
|
|
|
- //
|
|
|
- let index = scrollView.contentOffset.y / container.height
|
|
|
- scrollItem(to: Int(index), isChangePage: false)
|
|
|
- }
|
|
|
-
|
|
|
- func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
|
|
|
- //
|
|
|
- let index = scrollView.contentOffset.y / container.height
|
|
|
- scrollItem(to: Int(index), isChangePage: true)
|
|
|
-
|
|
|
-// WSSLog("🐱: 停止滚动++++++++++++++++++++++")
|
|
|
- }
|
|
|
-
|
|
|
- func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
|
|
|
- //
|
|
|
-// let index = scrollView.contentOffset.y / container.height
|
|
|
-// WSSLog("🐱\(#function): 当前页 >>> \(Int(index))")
|
|
|
- }
|
|
|
-}
|