CNCustomIndexView.swift
9.07 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//
// CNCustomIndexView.swift
// metaCode
//
// Created by daojian on 2023/5/31.
//
import Foundation
protocol CNCustomIndexViewDelegate:NSObjectProtocol{
///当前scrollView 发送滚动
func customIndexViewDidScroll(scrollView :UIScrollView);
}
class CNCustomIndexView:PGIndexBannerSubiew, UIScrollViewDelegate, WJColorChangeDelegate{
var colorChange: WJColorChange!
///代理
weak var delegate:CNCustomIndexViewDelegate?
///关联的控制器的view
weak var associatedVCView:UIView?
{
willSet
{
print("Will set an new value \(String(describing: newValue)) to age")
}
//我们需要在age属性发生变化后,更新一下nickName这个属性
didSet
{
self.addColorManager()
}
}
///父视图ScrollView
weak var superScrollView:UIScrollView?
// MARK: - 懒加载
///颜色数据
lazy var colorDataArr: NSMutableArray = {
let colorDataArr = NSMutableArray(array: NSArray(objects: UIColor.white,KVCBackgroupColor!,UIColor("#004FE0")!))
return colorDataArr
}()
///自己的ScrollView,用来装载子视图
lazy var baseScrollView:UIScrollView = {
let baseScrollView : UIScrollView = UIScrollView.init(frame: CGRect(x: 0, y: 0, width: self.width, height: self.height))
baseScrollView.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height*3)
baseScrollView.delegate = self
baseScrollView.bounces = false
baseScrollView.isPagingEnabled = true
baseScrollView.clipsToBounds = false
baseScrollView.showsHorizontalScrollIndicator = false
baseScrollView.showsVerticalScrollIndicator = false
if #available(iOS 11.0, *) {
//系统版本高于11.0
baseScrollView.contentInsetAdjustmentBehavior = .never
} else {
//系统版本低于11.0
baseScrollView.automaticallyAdjustsScrollIndicatorInsets = false
}
return baseScrollView
}()
///中间主界面
lazy var mainView:CNMainView = {
let W = self.width
let H = W*1.5
let mainView = CNMainView.init(frame: CGRectMake(0, self.frame.size.height + (self.frame.size.height-H)/2,W, H))
return mainView
}()
///收钱view
lazy var collectMoneyCodeView: CNCollectMoneyCodeView = {
let collectionView = CNCollectMoneyCodeView.init(frame: CGRectMake(-25, 0, self.frame.size.width + 50, self.frame.size.height))
return collectionView
}()
///付钱view
lazy var payMoneyCodeView : CNPayMoneyCodeView = {
let payMoneyCodeView = CNPayMoneyCodeView(frame: CGRectMake(-25, self.frame.size.height*2 + 150,self.frame.size.width+50, self.frame.size.height - 150))
return payMoneyCodeView
}()
///底部付钱提前view
lazy var bottomPayMoneyRemindView: CNBottomPayMoneyRemindView = {
let W: CGFloat = UIScreen.main.bounds.width-100
let bottomView = CNBottomPayMoneyRemindView.init(frame: CGRectMake(25, self.baseScrollView.height*2, W,W/2))
bottomView.backgroundColor = UIColor.clear
return bottomView
}()
/// 顶部收钱提前view
lazy var topCollectMoneyRemindView:CNTopCollectMoneyRemindView = {
let W: CGFloat = UIScreen.main.bounds.width-100
let topView = CNTopCollectMoneyRemindView.init(frame: CGRectMake(25, self.baseScrollView.height-W/2, W,W/2))
topView.backgroundColor = .clear
return topView
}()
// MARK: - 初始化方法
override init(frame: CGRect) {
super.init(frame: frame)
self.initUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - 添加UI
func initUI(){
self.addSubview(self.baseScrollView)
self.baseScrollView.addSubview(self.collectMoneyCodeView)
self.baseScrollView.addSubview(self.mainView)
self.baseScrollView.addSubview(self.payMoneyCodeView)
self.baseScrollView.addSubview(self.bottomPayMoneyRemindView)
self.baseScrollView.addSubview(self.topCollectMoneyRemindView)
//默认偏移到中间
self.baseScrollView.setContentOffset(CGPointMake(0, self.frame.size.height), animated: false)
}
// // MARK: - 手势
// func addCustomGestureRecognizer(){
// let singleTap: UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(singleCellTapAction))
//
// self.addGestureRecognizer(singleTap)
// }
// MARK: - 设置颜色值跟随ScrollView滑动变化
func addColorManager()
{
print(self.colorDataArr)
let colorChange:WJColorChange = WJColorChange.init(type:SingleColor)
colorChange.delegate = self
colorChange.offset = 100
colorChange.settingScrollView(self.baseScrollView, colorArray: self.colorDataArr as! [UIColor], needChangeColorView: self.associatedVCView)
self.colorChange = colorChange
}
// MARK: - WJColorChange代理
///WJColorChange 代理方法 滚动时候回调 传出当前的滚动视图
func wjColorScrollViewScroll(_ scrollView: UIScrollView!) {
let tag: NSInteger = self.tag
let defaultOffset: CGFloat = self.frame.size.height
let currentOffset: CGFloat = scrollView.contentOffset.y
/**隐藏左右view*/
let fabsOffset: CGFloat = abs(defaultOffset - currentOffset)
let leftView:UIView? = self.superScrollView?.viewWithTag(tag-1)
let rightView:UIView? = self.superScrollView?.viewWithTag(tag+1)
if fabsOffset > 70 {
leftView?.alpha = 0
rightView?.alpha = 0
}
else{
leftView?.alpha = (70-fabsOffset)/70.00
rightView?.alpha = (70-fabsOffset)/70.00
}
//上滑滑动到临近值,让bottomPayMoneyRemindView位置一直停留在底部,并慢慢消失
let bottomPayMoneyRemindViewMaxOffset: CGFloat = self.baseScrollView.height+self.bottomPayMoneyRemindView.height
if (currentOffset > bottomPayMoneyRemindViewMaxOffset) {
/**让bottomPayMoneyRemindView位置一直停留在底部*/
self.bottomPayMoneyRemindView.top = currentOffset+self.baseScrollView.height-self.bottomPayMoneyRemindView.height
/**渐渐隐藏bottomPayMoneyRemindView,缓冲距离为40*/
self.bottomPayMoneyRemindView.alpha = (40-(currentOffset - bottomPayMoneyRemindViewMaxOffset))/40.00
//让系统及时更新bottomPayMoneyRemindView
// self.bottomPayMoneyRemindView.setNeedsLayout()
// self.bottomPayMoneyRemindView.layoutIfNeeded()
}
//下滑滑动到临近值,让topCollectMoneyRemindView位置一直停留在底部,并慢慢消失
let topCollectMoneyRemindViewMinOffset: CGFloat = self.baseScrollView.height-self.topCollectMoneyRemindView.height
if (currentOffset < topCollectMoneyRemindViewMinOffset) {
/**让topCollectMoneyRemindView位置一直停留在底部*/
self.topCollectMoneyRemindView.bottom = currentOffset+self.topCollectMoneyRemindView.height
/**渐渐隐藏topCollectMoneyRemindView,缓冲距离为40*/
print("currentOffset =", topCollectMoneyRemindViewMinOffset - currentOffset)
self.topCollectMoneyRemindView.alpha = (40-(topCollectMoneyRemindViewMinOffset - currentOffset))/40.00
//让系统及时更新topCollectMoneyRemindView
// self.topCollectMoneyRemindView.setNeedsLayout()
// self.topCollectMoneyRemindView.layoutIfNeeded()
}
/**控制缩放范围在一定范围*/
// let mainViewOffset:CGFloat = abs(currentOffset - self.baseScrollView.height)
// if (mainViewOffset < 100) {
// var scale: CGFloat = 1.0
//
// if (currentOffset < self.baseScrollView.height) {
// //下滑时,计算scale方法
// scale = currentOffset/self.baseScrollView.height
// }
// else {
// //上滑时,计算scale方法
// scale = self.baseScrollView.height/currentOffset
// }
//
// self.mainView.scaledView(scale: scale)
// }
delegate?.customIndexViewDidScroll(scrollView: self.baseScrollView)
}
///WJColorChange 代理方法 滚动结束时候回调
func wjColorEndScroll(_ scrollView: UIScrollView!) {
let defaultOffset: CGFloat = self.frame.size.height
let currentOffset: CGFloat = scrollView.contentOffset.y
if ((currentOffset < (defaultOffset + 1)) &&
(currentOffset > (defaultOffset - 1))
) {
self.superScrollView?.isScrollEnabled = true
}
else{
self.superScrollView?.isScrollEnabled = false
}
}
}