-
Notifications
You must be signed in to change notification settings - Fork 24
/
CustomizableActionSheet.swift
283 lines (245 loc) · 8.6 KB
/
CustomizableActionSheet.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
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
//
// CustomizableActionSheet.swift
// CustomizableActionSheet
//
// Created by Ryuta Kibe on 2015/12/22.
// Copyright 2015 blk. All rights reserved.
//
import UIKit
@objc public enum CustomizableActionSheetItemType: Int {
case button
case view
}
// Can't define CustomizableActionSheetItem as struct because Obj-C can't see struct definition.
public class CustomizableActionSheetItem: NSObject {
// MARK: - Public properties
public var type: CustomizableActionSheetItemType = .button
public var height: CGFloat = CustomizableActionSheetItem.kDefaultHeight
public static let kDefaultHeight: CGFloat = 44
// type = .View
public var view: UIView?
// type = .Button
public var label: String?
public var textColor: UIColor = UIColor(red: 0, green: 0.47, blue: 1.0, alpha: 1.0)
public var backgroundColor: UIColor = UIColor.white
public var font: UIFont? = nil
public var selectAction: ((_ actionSheet: CustomizableActionSheet) -> Void)? = nil
// MARK: - Private properties
fileprivate var element: UIView? = nil
public convenience init(type: CustomizableActionSheetItemType,
height: CGFloat = CustomizableActionSheetItem.kDefaultHeight) {
self.init()
self.type = type
self.height = height
}
}
private class ActionSheetItemView: UIView {
var subview: UIView?
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.setting()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.setting()
}
init() {
super.init(frame: CGRect.zero)
self.setting()
}
func setting() {
self.clipsToBounds = true
}
override func addSubview(_ view: UIView) {
super.addSubview(view)
self.subview = view
}
override func layoutSubviews() {
super.layoutSubviews()
if let subview = self.subview {
subview.frame = self.bounds
}
}
}
@objc public enum CustomizableActionSheetPosition: Int {
case bottom
case top
}
public class CustomizableActionSheet: NSObject {
// MARK: - Private properties
private static var actionSheets = [CustomizableActionSheet]()
private static let kMarginSide: CGFloat = 8
private static let kItemInterval: CGFloat = 8
private static let kMarginTop: CGFloat = 20
private var items: [CustomizableActionSheetItem]?
private let maskView = UIView()
private let itemContainerView = UIView()
private var closeBlock: (() -> Void)?
// MARK: - Public properties
public var defaultCornerRadius: CGFloat = 4
public var position: CustomizableActionSheetPosition = .bottom
public func showInView(_ targetView: UIView, items: [CustomizableActionSheetItem], closeBlock: (() -> Void)? = nil) {
// Save instance to reaction until closing this sheet
CustomizableActionSheet.actionSheets.append(self)
let targetBounds = targetView.bounds
// Save closeBlock
self.closeBlock = closeBlock
// mask view
let maskViewTapGesture = UITapGestureRecognizer(target: self, action: #selector(CustomizableActionSheet.maskViewWasTapped))
self.maskView.addGestureRecognizer(maskViewTapGesture)
self.maskView.frame = targetBounds
self.maskView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
targetView.addSubview(self.maskView)
// set items
for subview in self.itemContainerView.subviews {
subview.removeFromSuperview()
}
var currentPosition: CGFloat = 0
let safeAreaLeft: CGFloat
let safeAreaWidth: CGFloat
let safeAreaTop: CGFloat
let safeAreaBottom: CGFloat
if #available(iOS 11.0, *) {
safeAreaLeft = targetView.safeAreaInsets.left
safeAreaWidth = targetView.safeAreaLayoutGuide.layoutFrame.width
safeAreaTop = targetView.safeAreaInsets.top
safeAreaBottom = targetView.safeAreaInsets.bottom
} else {
safeAreaLeft = 0
safeAreaWidth = targetBounds.width
safeAreaTop = CustomizableActionSheet.kMarginTop
safeAreaBottom = 0
}
var availableHeight = targetBounds.height - safeAreaTop - safeAreaBottom
// Calculate height of items
for item in items {
availableHeight = availableHeight - item.height - CustomizableActionSheet.kItemInterval
}
for item in items {
// Apply height of items
if availableHeight < 0 {
let reduceNum = min(item.height, -availableHeight)
item.height -= reduceNum
availableHeight += reduceNum
if item.height <= 0 {
availableHeight += CustomizableActionSheet.kItemInterval
continue
}
}
// Add views
switch (item.type) {
case .button:
let button = UIButton()
button.layer.cornerRadius = defaultCornerRadius
button.frame = CGRect(
x: CustomizableActionSheet.kMarginSide,
y: currentPosition,
width: safeAreaWidth - (CustomizableActionSheet.kMarginSide * 2),
height: item.height)
button.setTitle(item.label, for: UIControl.State())
button.backgroundColor = item.backgroundColor
button.setTitleColor(item.textColor, for: UIControl.State())
if let font = item.font {
button.titleLabel?.font = font
}
if let _ = item.selectAction {
button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(CustomizableActionSheet.buttonWasTapped(_:))))
}
item.element = button
self.itemContainerView.addSubview(button)
currentPosition = currentPosition + item.height + CustomizableActionSheet.kItemInterval
case .view:
if let view = item.view {
let containerView = ActionSheetItemView(frame: CGRect(
x: CustomizableActionSheet.kMarginSide,
y: currentPosition,
width: safeAreaWidth - (CustomizableActionSheet.kMarginSide * 2),
height: item.height))
containerView.layer.cornerRadius = defaultCornerRadius
containerView.addSubview(view)
view.frame = view.bounds
self.itemContainerView.addSubview(containerView)
item.element = view
currentPosition = currentPosition + item.height + CustomizableActionSheet.kItemInterval
}
}
}
let positionX: CGFloat = safeAreaLeft
var positionY: CGFloat = targetBounds.minY + targetBounds.height - currentPosition - safeAreaBottom
var moveY: CGFloat = positionY
if self.position == .top {
positionY = CustomizableActionSheet.kItemInterval
moveY = -currentPosition
}
self.itemContainerView.frame = CGRect(
x: positionX,
y: positionY,
width: safeAreaWidth,
height: currentPosition)
self.items = items
// Show animation
self.maskView.alpha = 0
targetView.addSubview(self.itemContainerView)
self.itemContainerView.transform = CGAffineTransform(translationX: 0, y: moveY)
UIView.animate(withDuration: 0.4,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: .curveEaseOut,
animations: { () -> Void in
self.maskView.alpha = 1
self.itemContainerView.transform = CGAffineTransform.identity
}, completion: nil)
}
public func dismiss() {
guard let targetView = self.itemContainerView.superview else {
return
}
// Hide animation
self.maskView.alpha = 1
var moveY: CGFloat = targetView.bounds.height - self.itemContainerView.frame.origin.y
if self.position == .top {
moveY = -self.itemContainerView.frame.height
}
UIView.animate(withDuration: 0.2,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: .curveEaseOut,
animations: { () -> Void in
self.maskView.alpha = 0
self.itemContainerView.transform = CGAffineTransform(translationX: 0, y: moveY)
}) { (result: Bool) -> Void in
// Remove views
self.itemContainerView.removeFromSuperview()
self.maskView.removeFromSuperview()
// Remove this instance
for i in 0 ..< CustomizableActionSheet.actionSheets.count {
if CustomizableActionSheet.actionSheets[i] == self {
CustomizableActionSheet.actionSheets.remove(at: i)
break
}
}
self.closeBlock?()
}
}
// MARK: - Private methods
@objc private func maskViewWasTapped() {
self.dismiss()
}
@objc private func buttonWasTapped(_ sender: AnyObject) {
guard let items = self.items else {
return
}
for item in items {
guard
let element = item.element,
let gestureRecognizer = sender as? UITapGestureRecognizer else {
continue
}
if element == gestureRecognizer.view {
item.selectAction?(self)
}
}
}
}