-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathLoadDataCustomViewController.swift
122 lines (99 loc) · 4.52 KB
/
LoadDataCustomViewController.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// LoadDataListCustomViewController.swift
// JXSegmentedView
//
// Created by jiaxin on 2019/1/7.
// Copyright © 2019 jiaxin. All rights reserved.
//
import UIKit
import JXSegmentedView
class LoadDataCustomViewController: UIViewController {
var segmentedDataSource: JXSegmentedTitleDataSource!
var segmentedView: JXSegmentedView!
var contentScrollView: UIScrollView!
var listVCArray = [LoadDataCustomListViewController]()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
//1、初始化JXSegmentedView
segmentedView = JXSegmentedView()
//2、配置数据源
//segmentedViewDataSource一定要通过属性强持有!!!!!!!!!
segmentedDataSource = JXSegmentedTitleDataSource()
segmentedDataSource.isTitleColorGradientEnabled = true
segmentedView.dataSource = segmentedDataSource
//3、配置指示器
let indicator = JXSegmentedIndicatorLineView()
indicator.indicatorWidth = JXSegmentedViewAutomaticDimension
indicator.lineStyle = .lengthen
segmentedView.indicators = [indicator]
//4、配置JXSegmentedView的属性
segmentedView.delegate = self
view.addSubview(segmentedView)
//5、初始化contentScrollView
contentScrollView = UIScrollView()
contentScrollView.isPagingEnabled = true
contentScrollView.showsVerticalScrollIndicator = false
contentScrollView.showsHorizontalScrollIndicator = false
contentScrollView.scrollsToTop = false
contentScrollView.bounces = false
//禁用automaticallyInset
automaticallyAdjustsScrollViewInsets = false
if #available(iOS 11.0, *) {
contentScrollView.contentInsetAdjustmentBehavior = .never
}
view.addSubview(contentScrollView)
//6、将contentScrollView和segmentedView.contentScrollView进行关联
segmentedView.contentScrollView = contentScrollView
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "刷新数据", style: UIBarButtonItem.Style.plain, target: self, action: #selector(reloadData))
reloadData()
}
@objc func reloadData() {
segmentedDataSource.titles = getRandomTitles()
segmentedView.defaultSelectedIndex = 0
segmentedView.reloadData()
for vc in listVCArray {
vc.view.removeFromSuperview()
}
listVCArray.removeAll()
for index in 0..<segmentedDataSource.titles.count {
let vc = LoadDataCustomListViewController.init(style: .plain)
vc.typeString = segmentedDataSource.titles[index]
vc.naviController = navigationController
contentScrollView.addSubview(vc.view)
listVCArray.append(vc)
// if pagingViewShouldRTLLayout() {
// pagingView(horizontalFlipForView: vc.view)
// }
}
view.setNeedsLayout()
listVCArray.first?.loadDataForFirst()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
segmentedView.frame = CGRect(x: 0, y: 0, width: view.bounds.size.width, height: 50)
contentScrollView.frame = CGRect(x: 0, y: 50, width: view.bounds.size.width, height: view.bounds.size.height - 50)
contentScrollView.contentSize = CGSize(width: contentScrollView.bounds.size.width*CGFloat(segmentedDataSource.dataSource.count), height: contentScrollView.bounds.size.height)
for (index, vc) in listVCArray.enumerated() {
vc.view.frame = CGRect(x: contentScrollView.bounds.size.width*CGFloat(index), y: 0, width: contentScrollView.bounds.size.width, height: contentScrollView.bounds.size.height)
}
}
func getRandomTitles() -> [String] {
let titles = ["猴哥", "青蛙王子", "旺财", "粉红猪", "喜羊羊", "黄焖鸡", "小马哥", "牛魔王", "大象先生", "神龙"]
//随机title数量,4~n
let randomCount = Int(arc4random()%7 + 4)
var tempTitles = titles
var resultTitles = [String]()
for _ in 0..<randomCount {
let randomIndex = Int(arc4random()%UInt32(tempTitles.count))
resultTitles.append(tempTitles[randomIndex])
tempTitles.remove(at: randomIndex)
}
return resultTitles
}
}
extension LoadDataCustomViewController: JXSegmentedViewDelegate {
func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
listVCArray[index].loadDataForFirst()
}
}