/
SPIndicatorView.swift
594 lines (497 loc) · 21.8 KB
/
SPIndicatorView.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// The MIT License (MIT)
// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.io)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
#if os(iOS)
/**
SPIndicator: Main view. Can be customisable if need.
For change duration, check method `present` and pass duration and other specific property if need customise.
Here available set window on which shoud be present.
If you have some windows, you shoud configure it. Check property `presentWindow`.
For disable dismiss by drag, check property `.dismissByDrag`.
Recomended call `SPIndicator` and choose style func.
*/
@available(iOSApplicationExtension, unavailable)
open class SPIndicatorView: UIView {
// MARK: - UIAppearance
@objc dynamic open var duration: TimeInterval = 1.5
// MARK: - Properties
/**
SPIndicator: Change it for set `top` or `bottom` present side.
Shoud be change before present, instead of no effect.
*/
open var presentSide: SPIndicatorPresentSide = .top
/**
SPIndicator: By default allow drag indicator for hide.
While indicator is dragging, dismiss not work.
This behaviar can be disabled.
*/
open var dismissByDrag: Bool = true {
didSet {
setGesture()
}
}
/**
SPIndicator: Completion call after hide indicator.
*/
open var completion: (() -> Void)? = nil
// MARK: - Views
open var titleLabel: UILabel?
open var subtitleLabel: UILabel?
open var iconView: UIView?
private lazy var backgroundView: UIVisualEffectView = {
let view: UIVisualEffectView = {
if #available(iOS 13.0, *) {
return UIVisualEffectView(effect: UIBlurEffect(style: .systemThickMaterial))
} else {
return UIVisualEffectView(effect: UIBlurEffect(style: .light))
}
}()
view.isUserInteractionEnabled = false
return view
}()
weak open var presentWindow: UIWindow?
// MARK: - Init
public init(title: String, message: String? = nil, preset: SPIndicatorIconPreset) {
super.init(frame: CGRect.zero)
commonInit()
layout = SPIndicatorLayout(for: preset)
setTitle(title)
if let message = message {
setMessage(message)
}
setIcon(for: preset)
}
public init(title: String, message: String?) {
super.init(frame: CGRect.zero)
titleAreaFactor = 1.8
minimumAreaWidth = 100
commonInit()
layout = SPIndicatorLayout.message()
setTitle(title)
if let message = message {
setMessage(message)
}
}
public required init?(coder aDecoder: NSCoder) {
self.presentSide = .top
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
preservesSuperviewLayoutMargins = false
if #available(iOS 11.0, *) {
insetsLayoutMarginsFromSafeArea = false
}
backgroundColor = .clear
backgroundView.layer.masksToBounds = true
addSubview(backgroundView)
setShadow()
setGesture()
}
// MARK: - Configure
private func setTitle(_ text: String) {
let label = UILabel()
label.font = UIFont.preferredFont(forTextStyle: .footnote, weight: .semibold, addPoints: 0)
label.numberOfLines = 1
let style = NSMutableParagraphStyle()
style.lineBreakMode = .byTruncatingTail
style.lineSpacing = 3
label.attributedText = NSAttributedString(
string: text, attributes: [.paragraphStyle: style]
)
label.textAlignment = .center
label.textColor = UIColor.Compability.label.withAlphaComponent(0.6)
titleLabel = label
addSubview(label)
}
private func setMessage(_ text: String) {
let label = UILabel()
label.font = UIFont.preferredFont(forTextStyle: .footnote, weight: .semibold, addPoints: 0)
label.numberOfLines = 1
let style = NSMutableParagraphStyle()
style.lineBreakMode = .byTruncatingTail
style.lineSpacing = 2
label.attributedText = NSAttributedString(
string: text, attributes: [.paragraphStyle: style]
)
label.textAlignment = .center
label.textColor = UIColor.Compability.label.withAlphaComponent(0.3)
subtitleLabel = label
addSubview(label)
}
private func setIcon(for preset: SPIndicatorIconPreset) {
let view = preset.createView()
self.iconView = view
addSubview(view)
}
private func setShadow() {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.22
layer.shadowOffset = .init(width: 0, height: 7)
layer.shadowRadius = 40
// Not use render shadow becouse backgorund is visual effect.
// If turn on it, background will hide.
// layer.shouldRasterize = true
}
private func setGesture() {
if dismissByDrag {
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
addGestureRecognizer(gestureRecognizer)
self.gestureRecognizer = gestureRecognizer
} else {
self.gestureRecognizer = nil
}
}
// MARK: - Present
private var presentAndDismissDuration: TimeInterval = 0.6
private var presentWithOpacity: Bool {
if presentSide == .center { return true }
return false
}
open func present(haptic: SPIndicatorHaptic = .success, completion: (() -> Void)? = nil) {
present(duration: self.duration, haptic: haptic, completion: completion)
}
open func present(duration: TimeInterval, haptic: SPIndicatorHaptic = .success, completion: (() -> Void)? = nil) {
if self.presentWindow == nil {
self.presentWindow = UIApplication.shared.keyWindow
}
guard let window = self.presentWindow else { return }
window.addSubview(self)
// Prepare for present
self.whenGestureEndShoudHide = false
self.completion = completion
isHidden = true
sizeToFit()
layoutSubviews()
center.x = window.frame.midX
toPresentPosition(.prepare(presentSide))
self.alpha = presentWithOpacity ? 0 : 1
// Present
isHidden = false
haptic.impact()
UIView.animate(withDuration: presentAndDismissDuration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: [.beginFromCurrentState, .curveEaseOut], animations: {
self.toPresentPosition(.visible(self.presentSide))
if self.presentWithOpacity { self.alpha = 1 }
}, completion: { finished in
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration) {
if self.gestureIsDragging {
self.whenGestureEndShoudHide = true
} else {
self.dismiss()
}
}
})
if let iconView = self.iconView as? SPIndicatorIconAnimatable {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + presentAndDismissDuration / 3) {
iconView.animate()
}
}
}
@objc open func dismiss() {
UIView.animate(withDuration: presentAndDismissDuration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: [.beginFromCurrentState, .curveEaseIn], animations: {
self.toPresentPosition(.prepare(self.presentSide))
if self.presentWithOpacity { self.alpha = 0 }
}, completion: { finished in
self.removeFromSuperview()
self.completion?()
})
}
// MARK: - Internal
private var minimumYTranslationForHideByGesture: CGFloat = -10
private var maximumYTranslationByGesture: CGFloat = 60
private var gestureRecognizer: UIPanGestureRecognizer?
private var gestureIsDragging: Bool = false
private var whenGestureEndShoudHide: Bool = false
@objc func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
self.gestureIsDragging = true
let translation = gestureRecognizer.translation(in: self)
let newTranslation: CGFloat = {
switch presentSide {
case .top:
if translation.y <= 0 {
return translation.y
} else {
return min(maximumYTranslationByGesture, translation.y.squareRoot())
}
case .bottom:
if translation.y >= 0 {
return translation.y
} else {
let absolute = abs(translation.y)
return -min(maximumYTranslationByGesture, absolute.squareRoot())
}
case .center:
let absolute = abs(translation.y).squareRoot()
let newValue = translation.y < 0 ? -absolute : absolute
return min(maximumYTranslationByGesture, newValue)
}
}()
toPresentPosition(.fromVisible(newTranslation, from: (presentSide)))
}
if gestureRecognizer.state == .ended {
gestureIsDragging = false
var shoudDismissWhenEndAnimation: Bool = false
UIView.animate(withDuration: presentAndDismissDuration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: [.beginFromCurrentState, .curveEaseIn], animations: {
if self.whenGestureEndShoudHide {
self.toPresentPosition(.prepare(self.presentSide))
shoudDismissWhenEndAnimation = true
} else {
let translation = gestureRecognizer.translation(in: self)
if translation.y < self.minimumYTranslationForHideByGesture {
self.toPresentPosition(.prepare(self.presentSide))
shoudDismissWhenEndAnimation = true
} else {
self.toPresentPosition(.visible(self.presentSide))
}
}
}, completion: { _ in
if shoudDismissWhenEndAnimation {
self.dismiss()
}
})
}
}
private func toPresentPosition(_ position: PresentPosition) {
let getPrepareTransform: ((_ side: SPIndicatorPresentSide) -> CGAffineTransform) = { [weak self] side in
guard let self = self else { return .identity }
guard let window = UIApplication.shared.windows.first else { return .identity }
switch side {
case .top:
let topInset = window.safeAreaInsets.top
let position = -(topInset + 50)
return CGAffineTransform.identity.translatedBy(x: 0, y: position)
case .bottom:
let height = window.frame.height
let bottomInset = window.safeAreaInsets.bottom
let position = height + bottomInset + 50
return CGAffineTransform.identity.translatedBy(x: 0, y: position)
case .center:
return CGAffineTransform.identity.translatedBy(x: 0, y: window.frame.height / 2 - self.frame.height / 2).scaledBy(x: 0.9, y: 0.9)
}
}
let getVisibleTransform: ((_ side: SPIndicatorPresentSide) -> CGAffineTransform) = { [weak self] side in
guard let self = self else { return .identity }
guard let window = UIApplication.shared.windows.first else { return .identity }
switch side {
case .top:
var topSafeAreaInsets = window.safeAreaInsets.top
if topSafeAreaInsets < 20 { topSafeAreaInsets = 20 }
let position = topSafeAreaInsets - 3 + self.offset
return CGAffineTransform.identity.translatedBy(x: 0, y: position)
case .bottom:
let height = window.frame.height
var bottomSafeAreaInsets = window.safeAreaInsets.top
if bottomSafeAreaInsets < 20 { bottomSafeAreaInsets = 20 }
let position = height - bottomSafeAreaInsets - 3 - self.frame.height - self.offset
return CGAffineTransform.identity.translatedBy(x: 0, y: position)
case .center:
return CGAffineTransform.identity.translatedBy(x: 0, y: window.frame.height / 2 - self.frame.height / 2)
}
}
switch position {
case .prepare(let presentSide):
transform = getPrepareTransform(presentSide)
case .visible(let presentSide):
transform = getVisibleTransform(presentSide)
case .fromVisible(let translation, let presentSide):
transform = getVisibleTransform(presentSide).translatedBy(x: 0, y: translation)
}
}
// MARK: - Layout
/**
SPIndicator: Wraper of layout values.
*/
open var layout: SPIndicatorLayout = .init()
/**
SPIndicator: Alert offset
*/
open var offset: CGFloat = 0
private var areaHeight: CGFloat = 50
private var minimumAreaWidth: CGFloat = 196
private var maximumAreaWidth: CGFloat = 260
private var titleAreaFactor: CGFloat = 2.5
private var spaceBetweenTitles: CGFloat = 1
private var spaceBetweenTitlesAndImage: CGFloat = 16
private var titlesCompactWidth: CGFloat {
if let iconView = self.iconView {
let space = iconView.frame.maxY + spaceBetweenTitlesAndImage
return frame.width - space * 2
} else {
return frame.width - layoutMargins.left - layoutMargins.right
}
}
private var titlesFullWidth: CGFloat {
if let iconView = self.iconView {
let space = iconView.frame.maxY + spaceBetweenTitlesAndImage
return frame.width - space - layoutMargins.right - self.spaceBetweenTitlesAndImage
} else {
return frame.width - layoutMargins.left - layoutMargins.right
}
}
open override func sizeThatFits(_ size: CGSize) -> CGSize {
titleLabel?.sizeToFit()
let titleWidth: CGFloat = titleLabel?.frame.width ?? 0
subtitleLabel?.sizeToFit()
let subtitleWidth: CGFloat = subtitleLabel?.frame.width ?? 0
var width = (max(titleWidth, subtitleWidth) * titleAreaFactor).rounded()
if width < minimumAreaWidth { width = minimumAreaWidth }
if width > maximumAreaWidth { width = maximumAreaWidth }
return .init(width: width, height: areaHeight)
}
open override func layoutSubviews() {
super.layoutSubviews()
layoutMargins = layout.margins
layer.cornerRadius = frame.height / 2
backgroundView.frame = bounds
backgroundView.layer.cornerRadius = layer.cornerRadius
// Flags
let hasIcon = (self.iconView != nil)
let hasTitle = (self.titleLabel != nil)
let hasSubtite = (self.subtitleLabel != nil)
let fitTitleToCompact: Bool = {
guard let titleLabel = self.titleLabel else { return true }
titleLabel.numberOfLines = 1
titleLabel.sizeToFit()
return titleLabel.frame.width < titlesCompactWidth
}()
let fitSubtitleToCompact: Bool = {
guard let subtitleLabel = self.subtitleLabel else { return true }
subtitleLabel.numberOfLines = 1
subtitleLabel.sizeToFit()
return subtitleLabel.frame.width < titlesCompactWidth
}()
let notFitAnyLabelToCompact: Bool = {
if !fitTitleToCompact { return true }
if !fitSubtitleToCompact { return true }
return false
}()
var layout: LayoutGrid = .iconTitleCentered
if (hasIcon && hasTitle && hasSubtite) && !notFitAnyLabelToCompact {
layout = .iconTitleMessageCentered
}
if (hasIcon && hasTitle && hasSubtite) && notFitAnyLabelToCompact {
layout = .iconTitleMessageLeading
}
if (hasIcon && hasTitle && !hasSubtite) {
layout = .iconTitleCentered
}
if (!hasIcon && hasTitle && !hasSubtite) {
layout = .title
}
if (!hasIcon && hasTitle && hasSubtite) {
layout = .titleMessage
}
// Actions
let layoutIcon = { [weak self] in
guard let self = self else { return }
guard let iconView = self.iconView else { return }
iconView.frame = .init(
origin: .init(x: self.layoutMargins.left, y: iconView.frame.origin.y),
size: self.layout.iconSize
)
iconView.center.y = self.bounds.midY
}
let layoutTitleCenteredCompact = { [weak self] in
guard let self = self else { return }
guard let titleLabel = self.titleLabel else { return }
titleLabel.textAlignment = .center
titleLabel.layoutDynamicHeight(width: self.titlesCompactWidth)
titleLabel.center.x = self.frame.width / 2
}
let layoutTitleCenteredFullWidth = { [weak self] in
guard let self = self else { return }
guard let titleLabel = self.titleLabel else { return }
titleLabel.textAlignment = .center
titleLabel.layoutDynamicHeight(width: self.titlesFullWidth)
titleLabel.center.x = self.frame.width / 2
}
let layoutTitleLeadingFullWidth = { [weak self] in
guard let self = self else { return }
guard let titleLabel = self.titleLabel else { return }
guard let iconView = self.iconView else { return }
let rtl = self.effectiveUserInterfaceLayoutDirection == .rightToLeft
titleLabel.textAlignment = rtl ? .right : .left
titleLabel.layoutDynamicHeight(width: self.titlesFullWidth)
titleLabel.frame.origin.x = self.layoutMargins.left + iconView.frame.width + self.spaceBetweenTitlesAndImage
}
let layoutSubtitle = { [weak self] in
guard let self = self else { return }
guard let titleLabel = self.titleLabel else { return }
guard let subtitleLabel = self.subtitleLabel else { return }
subtitleLabel.textAlignment = titleLabel.textAlignment
subtitleLabel.layoutDynamicHeight(width: titleLabel.frame.width)
subtitleLabel.frame.origin.x = titleLabel.frame.origin.x
}
let layoutTitleSubtitleByVertical = { [weak self] in
guard let self = self else { return }
guard let titleLabel = self.titleLabel else { return }
guard let subtitleLabel = self.subtitleLabel else {
titleLabel.center.y = self.bounds.midY
return
}
let allHeight = titleLabel.frame.height + subtitleLabel.frame.height + self.spaceBetweenTitles
titleLabel.frame.origin.y = (self.frame.height - allHeight) / 2
subtitleLabel.frame.origin.y = titleLabel.frame.maxY + self.spaceBetweenTitles
}
// Apply
switch layout {
case .iconTitleMessageCentered:
layoutIcon()
layoutTitleCenteredCompact()
layoutSubtitle()
case .iconTitleMessageLeading:
layoutIcon()
layoutTitleLeadingFullWidth()
layoutSubtitle()
case .iconTitleCentered:
layoutIcon()
titleLabel?.numberOfLines = 2
layoutTitleCenteredCompact()
case .iconTitleLeading:
layoutIcon()
titleLabel?.numberOfLines = 2
layoutTitleLeadingFullWidth()
case .title:
titleLabel?.numberOfLines = 2
layoutTitleCenteredFullWidth()
case .titleMessage:
layoutTitleCenteredFullWidth()
layoutSubtitle()
}
layoutTitleSubtitleByVertical()
}
// MARK: - Models
enum PresentPosition {
case prepare(_ from: SPIndicatorPresentSide)
case visible(_ from: SPIndicatorPresentSide)
case fromVisible(_ translation: CGFloat, from: SPIndicatorPresentSide)
}
enum LayoutGrid {
case iconTitleMessageCentered
case iconTitleMessageLeading
case iconTitleCentered
case iconTitleLeading
case title
case titleMessage
}
}
#endif