Commit 9bccb562 authored by David黄金龙's avatar David黄金龙

Merge branch 'davidhuang' into develop

* davidhuang:
  首页 UI 完善
  处理 编译不过的bug

# Conflicts:
#	galaxy/Podfile
parents 9d412b49 38195248
...@@ -29,7 +29,6 @@ target 'galaxy' do ...@@ -29,7 +29,6 @@ target 'galaxy' do
pod 'SwifterSwift','6.0.0' pod 'SwifterSwift','6.0.0'
#下拉刷新、上拉更多 #下拉刷新、上拉更多
pod 'ESPullToRefresh',"2.9.3" pod 'ESPullToRefresh',"2.9.3"
#Toast #Toast
pod 'Toast-Swift', '5.0.1' pod 'Toast-Swift', '5.0.1'
#富文本 UILabel #富文本 UILabel
...@@ -58,11 +57,12 @@ target 'galaxy' do ...@@ -58,11 +57,12 @@ target 'galaxy' do
pod 'SkeletonView','1.30.4' pod 'SkeletonView','1.30.4'
#导航栏、手势 #导航栏、手势
pod 'GKNavigationBarSwift','1.6.5' pod 'GKNavigationBarSwift','1.6.5'
#crash统计 #crash统计
pod 'Bugly','2.5.93' pod 'Bugly','2.5.93'
#神策数据分析 #神策数据分析
pod 'SensorsAnalyticsSDK' pod 'SensorsAnalyticsSDK','4.6.0'
#主流APP分类切换滚动视图
pod 'JXSegmentedView','1.3.0'
end end
......
...@@ -81,6 +81,7 @@ ...@@ -81,6 +81,7 @@
A5C5B2E82B4EC95600A7C5D1 /* YHDavidTestCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5C5B2E72B4EC95600A7C5D1 /* YHDavidTestCollectionViewCell.swift */; }; A5C5B2E82B4EC95600A7C5D1 /* YHDavidTestCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5C5B2E72B4EC95600A7C5D1 /* YHDavidTestCollectionViewCell.swift */; };
A5C5B2EA2B4ECA4D00A7C5D1 /* YHDavidModel0.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5C5B2E92B4ECA4D00A7C5D1 /* YHDavidModel0.swift */; }; A5C5B2EA2B4ECA4D00A7C5D1 /* YHDavidModel0.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5C5B2E92B4ECA4D00A7C5D1 /* YHDavidModel0.swift */; };
A5C5B2EE2B4F9B8800A7C5D1 /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = A5C5B2ED2B4F9B8800A7C5D1 /* Localizable.xcstrings */; }; A5C5B2EE2B4F9B8800A7C5D1 /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = A5C5B2ED2B4F9B8800A7C5D1 /* Localizable.xcstrings */; };
A5C5B2F92B5515CE00A7C5D1 /* BsPageContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5C5B2F82B5515CE00A7C5D1 /* BsPageContentView.swift */; };
A5D6AB1B2B46A1CC001C10A5 /* YHHomeModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5D6AB1A2B46A1CC001C10A5 /* YHHomeModel.swift */; }; A5D6AB1B2B46A1CC001C10A5 /* YHHomeModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5D6AB1A2B46A1CC001C10A5 /* YHHomeModel.swift */; };
A5D6AB1D2B46AAFB001C10A5 /* YHHomePageViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5D6AB1C2B46AAFB001C10A5 /* YHHomePageViewModel.swift */; }; A5D6AB1D2B46AAFB001C10A5 /* YHHomePageViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5D6AB1C2B46AAFB001C10A5 /* YHHomePageViewModel.swift */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
...@@ -196,6 +197,7 @@ ...@@ -196,6 +197,7 @@
A5C5B2E72B4EC95600A7C5D1 /* YHDavidTestCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHDavidTestCollectionViewCell.swift; sourceTree = "<group>"; }; A5C5B2E72B4EC95600A7C5D1 /* YHDavidTestCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHDavidTestCollectionViewCell.swift; sourceTree = "<group>"; };
A5C5B2E92B4ECA4D00A7C5D1 /* YHDavidModel0.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHDavidModel0.swift; sourceTree = "<group>"; }; A5C5B2E92B4ECA4D00A7C5D1 /* YHDavidModel0.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHDavidModel0.swift; sourceTree = "<group>"; };
A5C5B2ED2B4F9B8800A7C5D1 /* Localizable.xcstrings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = "<group>"; }; A5C5B2ED2B4F9B8800A7C5D1 /* Localizable.xcstrings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = "<group>"; };
A5C5B2F82B5515CE00A7C5D1 /* BsPageContentView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BsPageContentView.swift; sourceTree = "<group>"; };
A5D6AB1A2B46A1CC001C10A5 /* YHHomeModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHHomeModel.swift; sourceTree = "<group>"; }; A5D6AB1A2B46A1CC001C10A5 /* YHHomeModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHHomeModel.swift; sourceTree = "<group>"; };
A5D6AB1C2B46AAFB001C10A5 /* YHHomePageViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHHomePageViewModel.swift; sourceTree = "<group>"; }; A5D6AB1C2B46AAFB001C10A5 /* YHHomePageViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHHomePageViewModel.swift; sourceTree = "<group>"; };
B6BB01E4C5DB87B66F0ECE84 /* Pods-galaxy.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-galaxy.release.xcconfig"; path = "Target Support Files/Pods-galaxy/Pods-galaxy.release.xcconfig"; sourceTree = "<group>"; }; B6BB01E4C5DB87B66F0ECE84 /* Pods-galaxy.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-galaxy.release.xcconfig"; path = "Target Support Files/Pods-galaxy/Pods-galaxy.release.xcconfig"; sourceTree = "<group>"; };
...@@ -597,6 +599,7 @@ ...@@ -597,6 +599,7 @@
A5ACE95B2B45712D002C94D2 /* V */ = { A5ACE95B2B45712D002C94D2 /* V */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
A5C5B2F82B5515CE00A7C5D1 /* BsPageContentView.swift */,
A530268F2B4E6F2700F35102 /* YHHomeCustomCell.swift */, A530268F2B4E6F2700F35102 /* YHHomeCustomCell.swift */,
A5C5B2E72B4EC95600A7C5D1 /* YHDavidTestCollectionViewCell.swift */, A5C5B2E72B4EC95600A7C5D1 /* YHDavidTestCollectionViewCell.swift */,
); );
...@@ -825,6 +828,7 @@ ...@@ -825,6 +828,7 @@
A5ACE9522B4564F7002C94D2 /* YHIntelligentServiceViewController.swift in Sources */, A5ACE9522B4564F7002C94D2 /* YHIntelligentServiceViewController.swift in Sources */,
A5ACE9462B4564F7002C94D2 /* BsHUDSquareBaseView.swift in Sources */, A5ACE9462B4564F7002C94D2 /* BsHUDSquareBaseView.swift in Sources */,
A5551FFE2B4C26CE00510980 /* YHBaseViewModel.swift in Sources */, A5551FFE2B4C26CE00510980 /* YHBaseViewModel.swift in Sources */,
A5C5B2F92B5515CE00A7C5D1 /* BsPageContentView.swift in Sources */,
A5ACE94B2B4564F7002C94D2 /* BsHUDRotatingImageView.swift in Sources */, A5ACE94B2B4564F7002C94D2 /* BsHUDRotatingImageView.swift in Sources */,
A5ACE94F2B4564F7002C94D2 /* YhOverlayView.swift in Sources */, A5ACE94F2B4564F7002C94D2 /* YhOverlayView.swift in Sources */,
A53026902B4E6F2700F35102 /* YHHomeCustomCell.swift in Sources */, A53026902B4E6F2700F35102 /* YHHomeCustomCell.swift in Sources */,
......
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
// //
import UIKit import UIKit
import ESPullToRefresh import ESPullToRefresh
import JXSegmentedView
class YHDavidTestViewController: YHBaseViewController { class YHDavidTestViewController: YHBaseViewController {
lazy var items = { lazy var items = {
...@@ -27,7 +27,7 @@ class YHDavidTestViewController: YHBaseViewController { ...@@ -27,7 +27,7 @@ class YHDavidTestViewController: YHBaseViewController {
flowLayout.scrollDirection = .vertical flowLayout.scrollDirection = .vertical
let collectinoView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout) let collectinoView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
collectinoView.contentInset = UIEdgeInsets(top: 0, left: margin, bottom: 0, right: margin) collectinoView.contentInset = UIEdgeInsets(top: 20, left: margin, bottom: 20, right: margin)
collectinoView.backgroundColor = UIColor.white collectinoView.backgroundColor = UIColor.white
collectinoView.register(YHAboutUsAdvantageCell.self, forCellWithReuseIdentifier:YHAboutUsAdvantageCell.cellReuseIdentifier) collectinoView.register(YHAboutUsAdvantageCell.self, forCellWithReuseIdentifier:YHAboutUsAdvantageCell.cellReuseIdentifier)
collectinoView.register(YHDavidTestCollectionViewCell.self, forCellWithReuseIdentifier:YHDavidTestCollectionViewCell.cellReuseIdentifier) collectinoView.register(YHDavidTestCollectionViewCell.self, forCellWithReuseIdentifier:YHDavidTestCollectionViewCell.cellReuseIdentifier)
...@@ -43,16 +43,12 @@ class YHDavidTestViewController: YHBaseViewController { ...@@ -43,16 +43,12 @@ class YHDavidTestViewController: YHBaseViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
setupUI() setupUI()
self.myCollectView.es.addPullToRefresh { view.backgroundColor = .mainColor//for test hjl
self.loadFirstItem() // gk_navigationBar.isHidden = true
}
self.myCollectView.es.startPullToRefresh()
self.myCollectView.es.addInfiniteScrolling {
self.loadFakeData()
}
} }
} }
...@@ -80,12 +76,22 @@ extension YHDavidTestViewController { ...@@ -80,12 +76,22 @@ extension YHDavidTestViewController {
func setupUI() { func setupUI() {
gk_navTitle = "David的测试UI" gk_navTitle = "David的测试UI"
view.backgroundColor = .white view.backgroundColor = .white
view.addSubview(myCollectView)
view.addSubview(myCollectView)
myCollectView.snp.makeConstraints { make in myCollectView.snp.makeConstraints { make in
make.left.right.bottom.equalToSuperview() make.left.right.bottom.equalToSuperview()
make.top.equalToSuperview().offset(k_Height_NavigationtBarAndStatuBar) make.top.equalToSuperview()//.offset(k_Height_NavigationtBarAndStatuBar)
}
self.myCollectView.es.addPullToRefresh {
self.loadFirstItem()
}
self.myCollectView.es.startPullToRefresh()
self.myCollectView.es.addInfiniteScrolling {
self.loadFakeData()
} }
} }
} }
...@@ -108,3 +114,11 @@ extension YHDavidTestViewController: UICollectionViewDelegateFlowLayout, UIColle ...@@ -108,3 +114,11 @@ extension YHDavidTestViewController: UICollectionViewDelegateFlowLayout, UIColle
print("index is \(indexPath.row)") print("index is \(indexPath.row)")
} }
} }
// MARK: - JXSegmentedListContainerViewListDelegate
extension YHDavidTestViewController: JXSegmentedListContainerViewListDelegate {
func listView() -> UIView {
return self.view
}
}
...@@ -8,8 +8,19 @@ ...@@ -8,8 +8,19 @@
import Foundation import Foundation
import GKNavigationBarSwift import GKNavigationBarSwift
import JXSegmentedView
class YHHomeViewController: YHBaseViewController { class YHHomeViewController: YHBaseViewController {
let sectionHeaderHeight : CGFloat = 40
let cellHeight : CGFloat = 80
var arrItemVc : [UIViewController] = []
var myCellHeight : CGFloat {
get {
return view.height - homeNavBarView.height - k_TabBar_Height - k_Height_NavigationtBarAndStatuBar - cellHeight * 2 - sectionHeaderHeight
}
}
//model 及网络请求 //model 及网络请求
lazy var homeReqVM : YHHomePageViewModel = { lazy var homeReqVM : YHHomePageViewModel = {
return YHHomePageViewModel() return YHHomePageViewModel()
...@@ -39,7 +50,6 @@ class YHHomeViewController: YHBaseViewController { ...@@ -39,7 +50,6 @@ class YHHomeViewController: YHBaseViewController {
make.width.equalTo(120) make.width.equalTo(120)
} }
let btnMsg = UIButton(type: .custom) let btnMsg = UIButton(type: .custom)
btnMsg.setImage(UIImage(named: "home_nav_msg"), for: .normal) btnMsg.setImage(UIImage(named: "home_nav_msg"), for: .normal)
view1.addSubview(btnMsg) view1.addSubview(btnMsg)
...@@ -66,7 +76,7 @@ class YHHomeViewController: YHBaseViewController { ...@@ -66,7 +76,7 @@ class YHHomeViewController: YHBaseViewController {
var homeTableView: UITableView = { var homeTableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .plain) let tableView = UITableView(frame: .zero, style: .grouped)
tableView.backgroundColor = .clear tableView.backgroundColor = .clear
tableView.separatorStyle = .none tableView.separatorStyle = .none
tableView.showsHorizontalScrollIndicator = false tableView.showsHorizontalScrollIndicator = false
...@@ -78,6 +88,53 @@ class YHHomeViewController: YHBaseViewController { ...@@ -78,6 +88,53 @@ class YHHomeViewController: YHBaseViewController {
}() }()
let arrItemTitles = ["推荐","子女教育","税务规划","民生医疗","成功案例","我的服务","行业资讯","香港生活","你我他","我是很长的tag呀"]
let segmentView : JXSegmentedView = JXSegmentedView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: 40))
lazy var segmentedDataSource: JXSegmentedBaseDataSource = {
let dataSource = JXSegmentedTitleDataSource()
dataSource.isTitleColorGradientEnabled = true
dataSource.titles = arrItemTitles
dataSource.titleNormalFont = UIFont.PFSCM(ofSize: 16)
dataSource.titleNormalColor = .commonColor25
dataSource.titleSelectedFont = UIFont.PFSCM(ofSize: 18)
dataSource.titleSelectedColor = .commonColor100
dataSource.isItemSpacingAverageEnabled = false
dataSource.itemWidth = JXSegmentedViewAutomaticDimension
dataSource.itemSpacing = 24
return dataSource
}()
//分类title 所对应的VC
lazy var listContainerView: JXSegmentedListContainerView! = {
return JXSegmentedListContainerView(dataSource: self)
}()
lazy var pageContentView: BsPageContentView = {
self.arrItemVc.removeAll()
for model in arrItemTitles {
let vc = YHDavidTestViewController()
vc.view.backgroundColor = .brown
arrItemVc.append(vc)
// //滑动使用
// vc.changeScrollBlock = { [weak self] in
// self?.canScroll = true
// }
}
let frame = CGRect(x: 0, y: 0, width: UIDevice.kScreenW, height: myCellHeight)
let view = BsPageContentView.init(frame: frame, childVcs: arrItemVc, parentViewController: self)
view.banScrollEnabled()
view.delegate = self
return view
}()
...@@ -90,13 +147,26 @@ class YHHomeViewController: YHBaseViewController { ...@@ -90,13 +147,26 @@ class YHHomeViewController: YHBaseViewController {
//2.网络请求 //2.网络请求
requestData() requestData()
} }
override func viewWillAppear(_ animated: Bool) { override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated) super.viewWillAppear(animated)
} }
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
view.backgroundColor = .white
segmentView.frame = CGRect(x: 0, y: 0, width: view.bounds.size.width, height: sectionHeaderHeight)
// listContainerView.frame = CGRect(x: 0, y: segmentedView.y + segmentedView.height, width: view.bounds.size.width, height: view.bounds.size.height - segmentedView.y - segmentedView.height - UIDevice.bs_tabBarFullHeight())
//
// printLog("segmentedView.frame.size.height ===> \(segmentedView.frame.size.height)")
// printLog("listContainerView.frame.size.height ===> \(listContainerView.frame.size.height)")
}
} }
// MARK: - 私有方法 // MARK: - 私有方法
...@@ -141,36 +211,135 @@ extension YHHomeViewController { ...@@ -141,36 +211,135 @@ extension YHHomeViewController {
make.top.equalTo(homeNavBarView.snp.bottom).offset(5) make.top.equalTo(homeNavBarView.snp.bottom).offset(5)
make.left.right.bottom.equalToSuperview() make.left.right.bottom.equalToSuperview()
} }
self.homeTableView.es.addPullToRefresh {
self.loadFirstItem()
}
self.homeTableView.es.startPullToRefresh()
self.homeTableView.es.addInfiniteScrolling {
self.loadFakeData()
}
//分类Title
segmentView.dataSource = segmentedDataSource
segmentView.delegate = self
//配置指示器
let indicator = JXSegmentedIndicatorLineView()
indicator.indicatorWidth = 16
indicator.indicatorColor = .blue
segmentView.indicators = [indicator]
//
arrItemVc.removeAll()
for title in arrItemTitles {
arrItemVc.append(YHDavidTestViewController())
}
}
private func loadFirstItem() {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.homeTableView.es.stopPullToRefresh(ignoreDate: true,ignoreFooter: false)
self.homeTableView.reloadData()
}
}
private func loadFakeData() {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.homeTableView.es.stopLoadingMore()
self.homeTableView.reloadData()
}
} }
@objc func tapSearch() { @objc func tapSearch() {
printLog("*") printLog("*")
navigationController?.pushViewController(YHDavidTestViewController()) navigationController?.pushViewController(YHDavidTestViewController())
} }
} }
// MARK: - UITableViewDelegate 和 UITableViewDataSource // MARK: - UITableViewDelegate 和 UITableViewDataSource
extension YHHomeViewController : UITableViewDelegate,UITableViewDataSource { extension YHHomeViewController : UITableViewDelegate,UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10 if section == 0 {
return 2
}
return 1
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 0
}
return sectionHeaderHeight
} }
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
return UIView()
}
segmentView.backgroundColor = .white
return segmentView
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension if indexPath.section == 0 {
return cellHeight
}
return myCellHeight
} }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// let cell0 = tableView.dequeueReusableCell(withIdentifier: YHHomeCustomCell.cellReuseIdentifier, for: indexPath) as! YHHomeCustomCell if indexPath.section == 0 {
// let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
// cell0.setData(["A","B","B","B","B","B","B","B","B","B","B","B","B","B","B"]) if indexPath.row == 0 {
// cell.contentView.backgroundColor = .systemPink
// return cell0 } else {
cell.contentView.backgroundColor = .brown
}
cell.textLabel?.text = "第\(indexPath.section + 1)个section - 第\(indexPath.row + 1) 行"
return cell
}
let cell = UITableViewCell()
cell.contentView.backgroundColor = .orange
return UITableViewCell() cell.contentView.addSubview(pageContentView)
return cell
} }
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
...@@ -178,5 +347,37 @@ extension YHHomeViewController : UITableViewDelegate,UITableViewDataSource { ...@@ -178,5 +347,37 @@ extension YHHomeViewController : UITableViewDelegate,UITableViewDataSource {
printLog("点击了 tableView Cell \(indexPath.row)") printLog("点击了 tableView Cell \(indexPath.row)")
} }
}
// MARK: - JXSegmentedViewDelegate
extension YHHomeViewController : JXSegmentedViewDelegate {
func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
printLog("选择了: \(index)")
}
}
// MARK: - JXSegmentedListContainerViewDataSource
extension YHHomeViewController: JXSegmentedListContainerViewDataSource {
func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
return arrItemTitles.count
}
func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
if index < arrItemVc.count {
return arrItemVc[index] as? JXSegmentedListContainerViewListDelegate ?? YHDavidTestViewController()
}
return YHDavidTestViewController()
}
}
// MARK: - BsPageContentViewDelegate
extension YHHomeViewController: BsPageContentViewDelegate {
func pageContentView(_ contentView: BsPageContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
// self.pageTitleView.setTitleWithProgress(progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
printLog("-----progress:\(progress) sourceIndex:\(sourceIndex) targetIndex:\(targetIndex)")
}
} }
//
// BsPageContentView.swift
// GDKit
//
// Created by GDKit on 01/11/2022.
// Copyright (c) 2022 GDKit. All rights reserved.
//
import UIKit
@objc public protocol BsPageContentViewDelegate : AnyObject {
func pageContentView(_ contentView : BsPageContentView, progress : CGFloat, sourceIndex : Int, targetIndex : Int)
}
private let ContentCellID = "BsContentCellID"
open class BsPageContentView: UIView {
// MARK: - 定义属性
fileprivate var childVcs : [UIViewController]
fileprivate weak var parentVC : UIViewController?
fileprivate var startOffsetX : CGFloat = 0
fileprivate var isForbidScrollDelegate : Bool = false
@objc public weak var delegate : BsPageContentViewDelegate?
public var isScrollEnabled: Bool? {
didSet {
if let isScrollEnabled = isScrollEnabled {
// collectionView.isScrollEnabled = isScrollEnabled
}
}
}
@objc public func banScrollEnabled() {
// collectionView.isScrollEnabled = false
collectionView.isScrollEnabled = true
}
fileprivate func getLayout() -> UICollectionViewFlowLayout {
let layout = UICollectionViewFlowLayout()
layout.itemSize = self.bounds.size
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.scrollDirection = .horizontal
return layout
}
// MARK: - 懒加载属性
fileprivate lazy var collectionView : UICollectionView = {[weak self] in
// 1.创建layout
let layout = self?.getLayout() ?? UICollectionViewFlowLayout()
// 2.创建UICollectionView
let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
collectionView.backgroundColor = .clear//groupTableViewBackground
collectionView.showsHorizontalScrollIndicator = false
collectionView.isPagingEnabled = true
collectionView.bounces = false
collectionView.dataSource = self
collectionView.delegate = self
collectionView.scrollsToTop = false
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: ContentCellID)
return collectionView
}()
// MARK: - 自定义构造函数
@objc public init(frame: CGRect, childVcs : [UIViewController], parentViewController : UIViewController?) {
self.childVcs = childVcs
self.parentVC = parentViewController
super.init(frame: frame)
// 设置UI
setupUI()
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// MARK: - 设置UI界面
extension BsPageContentView {
fileprivate func setupUI() {
// 1.将所有的子控制器添加父控制器中
// for childVc in childVcs {
// parentVC?.addChild(childVc)
// }
// 2.添加UICollectionView,用于在Cell中存放控制器的View
addSubview(collectionView)
collectionView.frame = bounds
}
public func refreshRect() {
collectionView.frame = bounds
collectionView.collectionViewLayout = self.getLayout()
}
}
// MARK: - 遵守UICollectionViewDataSource
extension BsPageContentView : UICollectionViewDataSource {
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return childVcs.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// 1.创建Cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ContentCellID, for: indexPath)
// 2.给Cell设置内容
for view in cell.contentView.subviews {
view.removeFromSuperview()
}
let childVc = childVcs[(indexPath as NSIndexPath).item]
childVc.view.frame = cell.contentView.bounds
cell.contentView.addSubview(childVc.view)
return cell
}
}
// MARK: - 遵守UICollectionViewDelegate
extension BsPageContentView : UICollectionViewDelegate {
public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
isForbidScrollDelegate = false
startOffsetX = scrollView.contentOffset.x
}
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
// 0.判断是否是点击事件
if isForbidScrollDelegate { return }
// 1.定义获取需要的数据
var progress : CGFloat = 0
var sourceIndex : Int = 0
var targetIndex : Int = 0
// 2.判断是左滑还是右滑
let currentOffsetX = scrollView.contentOffset.x
let scrollViewW = scrollView.bounds.width
if currentOffsetX > startOffsetX { // 左滑
// 1.计算progress
progress = currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW)
// 2.计算sourceIndex
sourceIndex = Int(currentOffsetX / scrollViewW)
// 3.计算targetIndex
targetIndex = sourceIndex + 1
if targetIndex >= childVcs.count {
targetIndex = childVcs.count - 1
}
// 4.如果完全划过去
if currentOffsetX - startOffsetX == scrollViewW {
progress = 1
targetIndex = sourceIndex
}
} else { // 右滑
// 1.计算progress
progress = 1 - (currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW))
// 2.计算targetIndex
targetIndex = Int(currentOffsetX / scrollViewW)
// 3.计算sourceIndex
sourceIndex = targetIndex + 1
if sourceIndex >= childVcs.count {
sourceIndex = childVcs.count - 1
}
}
// 3.将progress/sourceIndex/targetIndex传递给titleView
delegate?.pageContentView(self, progress: progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
}
}
// MARK: - 对外暴露的方法
extension BsPageContentView {
@objc public func setCurrentIndex(_ currentIndex : Int) {
// 1.记录需要进制执行代理方法
isForbidScrollDelegate = true
// 2.滚动正确的位置
let offsetX = CGFloat(currentIndex) * collectionView.frame.width
collectionView.setContentOffset(CGPoint(x: offsetX, y: 0), animated: false)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment