-
Notifications
You must be signed in to change notification settings - Fork 64
/
FWSheetView.swift
210 lines (161 loc) · 8.36 KB
/
FWSheetView.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
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
//
// FWSheetView.swift
// FWPopupView
//
// Created by xfg on 2018/3/26.
// Copyright © 2018年 xfg. All rights reserved.
//
import Foundation
import UIKit
open class FWSheetView: FWPopupView {
@objc open var property = FWSheetViewProperty()
private var actionItemArray: [FWPopupItem] = []
private var titleLabel: UILabel?
private var commponenetArray: [UIView] = []
/// 类初始化方法
///
/// - Parameters:
/// - title: 标题
/// - itemTitles: 点击项标题
/// - itemBlock: 点击回调
/// - cancenlBlock: 取消按钮回调
/// - Returns: self
@objc open class func sheet(title: String?, itemTitles: [String], itemBlock: FWPopupItemHandler? = nil, cancenlBlock: FWPopupVoidBlock? = nil) -> FWSheetView {
let sheetView = FWSheetView()
sheetView.setupUI(title: title, itemTitles: itemTitles, itemBlock:itemBlock, cancenlBlock: cancenlBlock)
return sheetView
}
/// 类初始化方法,可设置Sheet相关属性
///
/// - Parameters:
/// - title: 标题
/// - itemTitles: 点击项标题
/// - itemBlock: 点击回调
/// - cancenlBlock: 取消按钮回调
/// - property: FWSheetView的相关属性
/// - Returns: self
@objc open class func sheet(title: String?, itemTitles: [String], itemBlock: FWPopupItemHandler? = nil, cancenlBlock: FWPopupVoidBlock? = nil, property: FWSheetViewProperty?) -> FWSheetView {
let sheetView = FWSheetView()
if property != nil {
sheetView.property = property!
}
sheetView.setupUI(title: title, itemTitles: itemTitles, itemBlock:itemBlock, cancenlBlock: cancenlBlock)
return sheetView
}
}
extension FWSheetView {
private func setupUI(title: String?, itemTitles: [String], itemBlock: FWPopupItemHandler? = nil, cancenlBlock: FWPopupVoidBlock? = nil) {
if itemTitles.count == 0 {
return
}
self.backgroundColor = self.property.vbackgroundColor
self.clipsToBounds = true
self.popupType = .sheet
self.animationDuration = 0.3
let itemClickBlock: FWPopupItemHandler = { (index) in
if itemBlock != nil {
itemBlock!(index)
}
}
for title in itemTitles {
self.actionItemArray.append(FWPopupItem(title: title, itemType: .normal, isCancel: true, handler: itemClickBlock))
}
self.clipsToBounds = true
self.frame.origin.x = 0
self.frame.origin.y = 100
self.frame.size.width = UIScreen.main.bounds.width
self.setContentCompressionResistancePriority(.required, for: .horizontal)
self.setContentCompressionResistancePriority(.fittingSizeLevel, for: .vertical)
var currentMaxY:CGFloat = self.property.topBottomMargin
if title != nil && !title!.isEmpty {
self.titleLabel = UILabel(frame: CGRect(x: self.property.letfRigthMargin, y: currentMaxY, width: self.frame.width - self.property.letfRigthMargin * 2, height: CGFloat.greatestFiniteMagnitude))
self.addSubview(self.titleLabel!)
self.titleLabel?.text = title
self.titleLabel?.textColor = self.property.titleColor
self.titleLabel?.textAlignment = .center
self.titleLabel?.font = UIFont.boldSystemFont(ofSize: self.property.titleFontSize)
self.titleLabel?.numberOfLines = 5
self.titleLabel?.backgroundColor = UIColor.clear
self.titleLabel?.sizeToFit()
self.titleLabel?.frame = CGRect(x: self.property.letfRigthMargin, y: currentMaxY, width: self.frame.width - self.property.letfRigthMargin * 2, height: self.titleLabel!.frame.height)
currentMaxY = self.titleLabel!.frame.maxY
self.commponenetArray.append(self.titleLabel!)
}
currentMaxY += self.property.topBottomMargin
// 开始配置Item
let btnContrainerView = UIScrollView(frame: CGRect(x: 0, y: currentMaxY, width: self.frame.width, height: self.property.buttonHeight))
btnContrainerView.bounces = false
btnContrainerView.backgroundColor = UIColor.clear
self.addSubview(btnContrainerView)
currentMaxY = btnContrainerView.frame.maxY
let block: FWPopupItemHandler = { (index) in
if cancenlBlock != nil {
cancenlBlock!()
}
}
var tmpIndex = 0
self.actionItemArray.append(FWPopupItem(title: "取消", itemType: .normal, isCancel: true, handler: block))
var cancelBtnTopView: UIView?
var cancelBtn: UIButton?
for popupItem: FWPopupItem in self.actionItemArray {
let btn = UIButton(type: .custom)
btn.addTarget(self, action: #selector(btnAction(_:)), for: .touchUpInside)
btn.tag = tmpIndex
var btnY: CGFloat = 0.0
if tmpIndex < self.actionItemArray.count - 1 {
btnY = self.property.buttonHeight * CGFloat(tmpIndex)
btnContrainerView.addSubview(btn)
} else {
btnY = self.property.buttonHeight * CGFloat(tmpIndex) + self.property.cancelBtnMarginTop
cancelBtnTopView = UIView(frame: CGRect(x: 0, y: btnY - self.property.cancelBtnMarginTop, width: self.frame.width, height: self.property.cancelBtnMarginTop))
cancelBtnTopView?.backgroundColor = UIColor(white: 0.1, alpha: 0.1)
self.addSubview(cancelBtnTopView!)
cancelBtn = btn
self.addSubview(cancelBtn!)
}
btn.frame = CGRect(x: -self.property.splitWidth, y: btnY, width: btnContrainerView.frame.width + self.property.splitWidth * 2, height: self.property.buttonHeight + self.property.splitWidth)
if tmpIndex > 0 {
currentMaxY += btn.frame.height
if tmpIndex == self.actionItemArray.count - 1 {
if btn.frame.minY - self.property.cancelBtnMarginTop <= self.property.btnContrainerViewMaxHeight {
btnContrainerView.frame.size.height = btn.frame.minY - self.property.cancelBtnMarginTop
} else {
btnContrainerView.frame.size.height = self.property.btnContrainerViewMaxHeight
btnContrainerView.contentSize = CGSize(width: self.frame.width, height: btn.frame.minY - self.property.cancelBtnMarginTop)
}
cancelBtnTopView?.frame.origin.y = btnContrainerView.frame.maxY
cancelBtn?.frame.origin.y = cancelBtnTopView!.frame.maxY
}
}
btn.backgroundColor = self.property.vbackgroundColor
btn.setTitle(popupItem.title, for: .normal)
btn.setTitleColor(popupItem.highlight ? self.property.itemHighlightColor : self.property.itemNormalColor, for: .normal)
btn.layer.borderWidth = self.property.splitWidth
btn.layer.borderColor = self.property.splitColor.cgColor
btn.setBackgroundImage(self.getImageWithColor(color: btn.backgroundColor!), for: .normal)
btn.setBackgroundImage(self.getImageWithColor(color: self.property.itemPressedColor), for: .highlighted)
btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: self.property.buttonFontSize)
tmpIndex += 1
}
self.frame.size.height = btnContrainerView.frame.maxY + self.property.buttonHeight + self.property.cancelBtnMarginTop
}
}
extension FWSheetView {
@objc private func btnAction(_ sender: Any) {
let btn = sender as! UIButton
let item = self.actionItemArray[btn.tag]
if item.disabled {
return
}
self.hide()
if item.itemHandler != nil {
item.itemHandler!(btn.tag)
}
}
}
/// FWSheetView的相关属性
open class FWSheetViewProperty: FWPopupViewProperty {
// 取消按钮距离头部的距离
@objc public var cancelBtnMarginTop: CGFloat = 6
@objc public var btnContrainerViewMaxHeight: CGFloat = UIScreen.main.bounds.height * CGFloat(0.6)
}