CNCustomIndexView.swift
13.8 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//
// CNCustomIndexView.swift
// metaCode
//
// Created by daojian on 2023/5/31.
//
import Foundation
typealias closeBlock = ()->Void;
class CNCustomIndexTopView : UIView{
///关闭按钮事件回调
var closeAction: closeBlock?
//MARK: - 懒加载
///标题
lazy var titleLabel: UILabel = {
let titleLabel = UILabel.init(frame: CGRectMake(10, 0, 75, 35))
titleLabel.bottom = self.bottom - 5
titleLabel.textColor = .white
titleLabel.font = UIFont.systemFont(ofSize: 24, weight: .semibold)
return titleLabel
}()
///关闭按钮
lazy var closeBtn: UIButton = {
let closeBtn = UIButton(type: .custom)
closeBtn.frame = CGRect(x: self.width - 30, y: 0, width: 30, height: 30)
closeBtn.centerY = self.titleLabel.centerY
closeBtn.setImage(UIImage(named: "homePage_close_icon"), for: .normal)
closeBtn.addTarget(self, action: #selector(closeBtnAction), for: .touchUpInside)
return closeBtn
}()
//MARK: - 初始化
override init(frame: CGRect) {
super.init(frame: frame)
self.initUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func initUI(){
self.addSubview(self.titleLabel)
self.addSubview(self.closeBtn)
}
//MARK: - 公开方法
public func setTitleString(title:String)->Void{
self.titleLabel.text = title
}
//MARK: - 事件
@objc func closeBtnAction(){
closeAction?()
}
}
protocol CNCustomIndexViewDelegate:NSObjectProtocol{
/// 当前scrollView 将要开始滚动
func customIndexViewWillBeiginScroll(scrollView :UIScrollView);
///当前scrollView 滚动中
func customIndexViewDidScroll(scrollView :UIScrollView);
///当前scrollView 已经结束滚动
func customIndexViewDidEndScroll(scrollView :UIScrollView);
}
class CNCustomIndexView:PGIndexBannerSubiew, UIScrollViewDelegate, WJColorChangeDelegate{
///记录开始拖拽时偏移量
var beginDragOffset: CGFloat = 0.0
///颜色管理
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 + KSreenHeight*0.16,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))
collectionView.backAction = {[weak self] in
self?.baseScrollView.setContentOffset(CGPoint(x: 0, y: (self!.baseScrollView.height)), animated: true)
self?.superScrollView?.isScrollEnabled = true
let time = DispatchTime.now() + 0.5
DispatchQueue.main.asyncAfter(deadline:time, execute: DispatchWorkItem(block: {
//下滑提醒View回到初始位置
self?.topCollectMoneyRemindView.top = -((self?.topCollectMoneyRemindView.height)! + 50)
}))
}
return collectionView
}()
///付钱view
lazy var payMoneyCodeView : CNPayMoneyCodeView = {
let payMoneyCodeView = CNPayMoneyCodeView(frame: CGRectMake(-25, self.frame.size.height*2 + 120,self.frame.size.width+50, self.frame.size.height - 120))
return payMoneyCodeView
}()
///上滑付钱提醒view
lazy var bottomPayMoneyRemindView: CNBottomPayMoneyRemindView = {
let W: CGFloat = UIScreen.main.bounds.width-100
let H: CGFloat = W/2
let bottomView = CNBottomPayMoneyRemindView.init(frame: CGRectMake(25, self.height, W,H))
bottomView.backgroundColor = UIColor.clear
return bottomView
}()
/// 下滑收钱提醒view
lazy var topCollectMoneyRemindView:CNTopCollectMoneyRemindView = {
let W: CGFloat = UIScreen.main.bounds.width-100
let H: CGFloat = W/2
let topView = CNTopCollectMoneyRemindView.init(frame: CGRectMake(25, -H - 50, W,H))
topView.backgroundColor = .clear
return topView
}()
/// 顶部自定义导航
lazy var topNavView: CNCustomIndexTopView = {
let topNavView = CNCustomIndexTopView.init(frame: CGRect(x: 0, y: 0, width: self.width, height: KNavigationHeight))
topNavView.alpha = 0
topNavView.closeAction = { [weak self ] in
self!.baseScrollView.setContentOffset(CGPoint(x: 0, y: self!.baseScrollView.height), animated: true)
}
return topNavView
}()
// 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.bottomPayMoneyRemindView)
self.addSubview(self.topCollectMoneyRemindView)
self.addSubview(self.baseScrollView)
self.addSubview(self.topNavView)
self.topNavView.setTitleString(title: "志愿者")
self.baseScrollView.addSubview(self.collectMoneyCodeView)
self.baseScrollView.addSubview(self.mainView)
self.baseScrollView.addSubview(self.payMoneyCodeView)
//默认偏移到中间
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代理
///将要开始拖拽滚动
func wjColorScrollViewWillBeginDragging(_ scrollView: UIScrollView!) {
self.beginDragOffset = scrollView.contentOffset.y
self.delegate?.customIndexViewWillBeiginScroll(scrollView: scrollView)
//开始滑动把 baseScrollView 放到导航的上层
self.bringSubviewToFront(self.baseScrollView)
}
///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
}
//当self.baseScrollView的偏移量大于 self.baseScrollView.height时,显示上滑提醒view
if(currentOffset > self.baseScrollView.height){
self.bottomPayMoneyRemindView.top = self.height - (currentOffset - self.baseScrollView.height)*1.5
}
//上滑滑动到临近值,让bottomPayMoneyRemindView位置一直停留在底部,并慢慢消失
if (self.bottomPayMoneyRemindView.top < self.height - self.bottomPayMoneyRemindView.height) {
/**让bottomPayMoneyRemindView位置一直停留在底部*/
self.bottomPayMoneyRemindView.top = self.height - self.bottomPayMoneyRemindView.height
/**渐渐隐藏bottomPayMoneyRemindView,缓冲距离为20*/
let bottomPayMoneyRemindViewMaxOffset: CGFloat = self.baseScrollView.height+self.bottomPayMoneyRemindView.height
self.bottomPayMoneyRemindView.alpha = (20-(currentOffset - bottomPayMoneyRemindViewMaxOffset))/20.00
}
//当self.baseScrollView的偏移量小于 self.baseScrollView.height时,显示下滑提醒view
if(currentOffset < self.baseScrollView.height){
self.topCollectMoneyRemindView.bottom = (self.baseScrollView.height - currentOffset)*1.5
}
//下滑滑动到临近值,让topCollectMoneyRemindView位置一直停留在顶部,并慢慢消失
if (self.topCollectMoneyRemindView.bottom > self.topCollectMoneyRemindView.height) {
/**让topCollectMoneyRemindView位置一直停留在底部*/
self.topCollectMoneyRemindView.bottom = self.topCollectMoneyRemindView.height
/**渐渐隐藏topCollectMoneyRemindView,缓冲距离为20*/
let topCollectMoneyRemindViewMinOffset: CGFloat = self.baseScrollView.height-self.topCollectMoneyRemindView.height
self.topCollectMoneyRemindView.alpha = (20-(topCollectMoneyRemindViewMinOffset - currentOffset))/20.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)
// }
/**隐藏顶部导航**/
let topNavViewAlpha: CGFloat = 1.0 - (self.baseScrollView.height * 2 - currentOffset)/(self.baseScrollView.height*0.5)
if(topNavViewAlpha > 0){
self.topNavView.alpha = topNavViewAlpha
}
else{
self.topNavView.alpha = 0
}
//隐藏collectMoneyCodeView的bottomView
if scrollView.contentOffset.y > 10{
self.collectMoneyCodeView.bottomView.isHidden = true
self.collectMoneyCodeView.contentView.backgroundColor = .clear
}
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
}
if scrollView.contentOffset.y == 0{
//滚动到第一页,显示collectMoneyCodeView的底部view
self.collectMoneyCodeView.bottomView.isHidden = false
self.collectMoneyCodeView.contentView.backgroundColor = KVCBackgroupColor
}
//滑动结束 把topNavView 放到scrollView的上层
self.bringSubviewToFront(self.topNavView)
//下滑提醒View回到初始位置
self.topCollectMoneyRemindView.top = -(self.topCollectMoneyRemindView.height + 50)
//外部代理
self.delegate?.customIndexViewDidEndScroll(scrollView: scrollView)
}
}