CNUpSlideDetailScrollView.swift
7.93 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
//
// CNUpSlideDetailScrollView.swift
// metaCode
//
// Created by zx on 2023/10/16.
//
import UIKit
typealias enterBlock = (_ contentsMdl:CNLiveDownSlideContentsModel)->Void
class CNUpSlideDetailScrollView: UIScrollView {
//返回首页
var callBack : enterBlock?
let bgBackView = UIView()
let imageView = UIImageView()
//slideModel
var _slideModel : CNLiveDownSlideModel?
var slideModel : CNLiveDownSlideModel? {
get{
_slideModel
}
set{
_slideModel = newValue!
self.setupNewUI(model: _slideModel!)
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupUI() {
bgBackView.frame = CGRect(x: DownSlideRecommendLeftGap, y: 0,width:KSreenWidth , height:KSreenHeight)
imageView.frame = CGRect(x: 0,y: 0,width:KSreenWidth , height: KSreenWidth*DownSlideSingleCourseImgH/DownSlideSingleCourseW)
imageView.contentMode = .scaleAspectFill
bgBackView.addSubview(imageView)
addSubview(bgBackView)
contentSize = CGSize(width: KSreenWidth, height: bgBackView.height)
}
func setupNewUI(model:CNLiveDownSlideModel){
if model.type == SlideModelTypeAllCourse12 {
//全部课程列表
let topTitleLbl = UILabel(frame: CGRect(x: DownSlideRecommendLeftGap, y: KStatusBarHeight+DownSlideRecommendLeftGap, width: DownSlideSingleCourseW-DownSlideRecommendLeftGap*2, height: 30))
topTitleLbl.text = model.blockName ?? "全部课程"
topTitleLbl.font = BoldFont_21
topTitleLbl.textColor = HexColor("#333333")
bgBackView.addSubview(topTitleLbl)
//总块数
let contentsNum = model.contents.count
//model行数
let row = model.rowNum
//总显示块数
var totalNum = (contentsNum >= row) ? row : contentsNum
if row == 0{//row为0 , contents全部显示
totalNum = contentsNum
}
for i in 0..<totalNum{//i是第几行
let contentMdl = model.contents[i]
//小icon
let imageView = UIImageView(frame: CGRectMake(DownSlideRecommendLeftGap, topTitleLbl.bottom+DownSlideRecommendLeftGap+CGFloat(i)*(DownSlideAllCourseRowTopYGap+DownSlideAllCourseRowImageWH+DownSlideAllCourseRowBottomYGap), CGFloat(DownSlideAllCourseRowImageWH), CGFloat(DownSlideAllCourseRowImageWH)))
imageView.kf.setImage(with: URL(string:contentMdl.poster ?? ""))
bgBackView.addSubview(imageView)
let titleLbl = UILabel(frame: CGRect(x: imageView.right+DownSlideRecommendLeftGap, y: imageView.top, width: DownSlideSingleCourseW-90-imageView.right, height: 21))
titleLbl.text = contentMdl.title ?? "婴儿课程"
titleLbl.font = Font_15
titleLbl.textColor = HexColor("#333333")
bgBackView.addSubview(titleLbl)
let subTitleLbl = UILabel(frame: CGRect(x: titleLbl.left, y:titleLbl.bottom+3 , width: titleLbl.width, height: 17))
subTitleLbl.text = contentMdl.subTitle ?? "适应3-6岁"
subTitleLbl.font = Font_12
subTitleLbl.textColor = HexColor("#999999")
bgBackView.addSubview(subTitleLbl)
let enterBtn = UIButton(type: .custom)
enterBtn.frame = CGRect(x: subTitleLbl.right+10, y: 0, width: 65, height: 30)
enterBtn.centerY = imageView.centerY
enterBtn.setBackgroundImage(UIImage(named: "homepage_down_enter"), for: .normal)
enterBtn.setTitle("进入", for: .normal)
enterBtn.titleLabel?.font = BoldFont_13
enterBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
enterBtn.addTarget(self, action: #selector(enterBtnAction(_:)), for: .touchUpInside)
enterBtn.tag = i
bgBackView.addSubview(enterBtn)
}
}else if model.type == SlideModelTypeOneCourse75 {
//单图课程
let topTitleLbl = UILabel(frame: CGRect(x: 0, y: KStatusBarHeight+DownSlideRecommendLeftGap, width:bgBackView.width - 50 , height: 30))
topTitleLbl.text = model.contents[0].title ?? "婴儿课程"
topTitleLbl.font = BoldFont_21
topTitleLbl.textColor = HexColor("#FFFFFF")
imageView.addSubview(topTitleLbl)
let topSubTitleLbl = UILabel(frame: CGRect(x: topTitleLbl.left, y:topTitleLbl.bottom , width: topTitleLbl.width, height: 30))
topSubTitleLbl.text = model.contents[0].subTitle ?? "适应3-6岁"
topSubTitleLbl.font = Font_12
topSubTitleLbl.textColor = HexColor("#FFFFFF")
imageView.addSubview(topSubTitleLbl)
//总块数
let contentsNum = model.contents.count
//model行数
let row = model.rowNum
//总显示块数
var totalNum = (contentsNum >= row) ? row : contentsNum
if row == 0{//row为0 , contents全部显示
totalNum = contentsNum
}
for i in 0..<totalNum{//i是第几行
let contentMdl = model.contents[i]
//小icon
let imgView = UIImageView(frame: CGRectMake(0, imageView.height+DownSlideRecommendLeftGap+CGFloat(i)*(DownSlideAllCourseRowTopYGap+DownSlideAllCourseRowImageWH+DownSlideAllCourseRowBottomYGap), CGFloat(DownSlideAllCourseRowImageWH), CGFloat(DownSlideAllCourseRowImageWH)))
imgView.kf.setImage(with: URL(string:contentMdl.poster ?? ""))
bgBackView.addSubview(imgView)
let titleLbl = UILabel(frame: CGRect(x: imgView.right+DownSlideRecommendLeftGap, y: imgView.top, width: DownSlideSingleCourseW-90-imgView.right, height: 21))
titleLbl.text = contentMdl.title ?? "婴儿课程"
titleLbl.font = Font_15
titleLbl.textColor = HexColor("#333333")
bgBackView.addSubview(titleLbl)
let subTitleLbl = UILabel(frame: CGRect(x: titleLbl.left, y:titleLbl.bottom+3 , width: titleLbl.width, height: 17))
subTitleLbl.text = contentMdl.subTitle ?? "适应3-6岁"
subTitleLbl.font = Font_12
subTitleLbl.textColor = HexColor("#999999")
bgBackView.addSubview(subTitleLbl)
let enterBtn = UIButton(type: .custom)
enterBtn.frame = CGRect(x: subTitleLbl.right+10, y: 0, width: 65, height: 30)
enterBtn.centerY = imgView.centerY-5
enterBtn.setBackgroundImage(UIImage(named: "homepage_down_enter"), for: .normal)
enterBtn.setTitle("进入", for: .normal)
enterBtn.titleLabel?.font = BoldFont_13
enterBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
enterBtn.addTarget(self, action: #selector(enterBtnAction(_:)), for: .touchUpInside)
enterBtn.tag = i
bgBackView.addSubview(enterBtn)
}
}
}
//闭包:函数参数
// func callBackBlock(_ block: @escaping enterBlock) {
// block(_:)
// }
// func useBlock(){
// if callBack != nil {
// callBack!("闭包传值2")
// }
// }
//MARK: - 事件
@objc func enterBtnAction(_ sender: UIButton){
let tag = sender.tag
let contentMdl = (self.slideModel?.contents[tag])!
if callBack != nil {
let contentMdl1 = contentMdl
callBack!(contentMdl1)
}
}
}