-
Notifications
You must be signed in to change notification settings - Fork 5
/
ScrollContentIndicator.swift
101 lines (90 loc) · 3.18 KB
/
ScrollContentIndicator.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
//
// ScrollContentIndicator.swift
//
// Made with ❤️ by Novum
//
// Copyright © Telefonica. All rights reserved.
//
import UIKit
public class ScrollContentIndicator: Button {
private enum Constants {
static let shadowOpacity: Float = 0.2
static let shadowRadius: CGFloat = 12
static let defaultMargin: CGFloat = 24
static let defaultHeight: CGFloat = 40
static let animationDuration: TimeInterval = 0.3
}
public init(title: String, loadingTitle: String? = nil, isSmall: Bool = false) {
super.init(style: .primaryClear, title: title, loadingTitle: loadingTitle, isSmall: isSmall)
setUp()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
style = .primaryClear
setUp()
}
public func addToView(_ view: UIView) {
view.addSubview(self, constraints: [
centerXAnchor.constraint(equalTo: view.centerXAnchor),
heightAnchor.constraint(equalToConstant: Constants.defaultHeight),
topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: Constants.defaultMargin)
])
}
public func show() {
isHidden = false
UIView.animate(
withDuration: Constants.animationDuration,
delay: 0.0,
options: .curveEaseOut,
animations: {
self.transform = .identity
self.alpha = 1
}
)
}
public func hide() {
UIView.animate(
withDuration: Constants.animationDuration,
delay: 0.0,
options: .curveEaseIn,
animations: {
// Due to the maths behind Affine Transformation matrix we have to use 0.001 instead 0.0 for the down scale to work properly.
// More info: https://medium.com/@sarthak.tayade/problem-when-animating-a-uiview-to-scale-to-zero-using-cgaffinetransform-on-ios-2f50717add18
self.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
self.alpha = 0
},
completion: { _ in
self.isHidden = true
}
)
}
}
private extension ScrollContentIndicator {
var cornerRadius: CGFloat {
Constants.defaultHeight / 2
}
func setUp() {
removeBorder()
addBackgroundView()
makeRounded(cornerRadius: cornerRadius)
addShadow()
transform = CGAffineTransform(scaleX: 0.0, y: 0.0)
alpha = 0
isHidden = true
setContentCompressionResistancePriority(.required, for: .horizontal)
}
func addBackgroundView() {
let backgroundView = UIView()
backgroundView.backgroundColor = .brand
backgroundView.makeRounded(cornerRadius: cornerRadius)
backgroundView.isUserInteractionEnabled = false
insertSubview(backgroundView, at: 0, constraints: backgroundView.constraintsForEdges(to: self))
}
func addShadow() {
layer.shadowColor = UIColor.backgroundOverlay.cgColor
layer.shadowOpacity = Constants.shadowOpacity
layer.shadowOffset = .zero
layer.shadowRadius = Constants.shadowRadius
layer.masksToBounds = false
}
}