-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinimalisticTableView.swift
191 lines (142 loc) · 6.57 KB
/
MinimalisticTableView.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
//
// Minimalistic ScrollView.swift
//
// Created by Fredrik Bixo on 2017-01-21.
// Copyright © 2017 Fredrik Bixo. All rights reserved.
//
import UIKit
fileprivate var animateWidth:CGFloat = 250
// MARK: Delegate
extension MinimalisticTableView:UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
for view in self.subviews {
if let view = view as? AnimationView {
if view.frame.intersects(CGRect(origin: scrollView.contentOffset, size: CGSize(width: scrollView.frame.size.width+20, height: scrollView.frame.size.height))) {
// the view is visible
view.outAnimate()
} else {
view.inAnimate()
}
}
if let button = view as? UIButton {
if button.frame.intersects(CGRect(origin: scrollView.contentOffset, size: CGSize(width: scrollView.frame.size.width+20, height: scrollView.frame.size.height))) {
// the view is visible
UIView.animate(withDuration: 0.6, delay: 0.3, options: .curveEaseOut, animations: {
button.alpha = 1
}, completion: nil)
} else {
UIView.animate(withDuration: 0.6, delay: 0.3, options: .curveEaseOut, animations: {
button.alpha = 0
}, completion: nil)
}
}
if let label = view as? UILabel {
if label.frame.intersects(CGRect(origin: scrollView.contentOffset, size: CGSize(width: scrollView.frame.size.width+20, height: scrollView.frame.size.height))) {
// the view is visible
UIView.animate(withDuration: 0.6, delay: 0.3, options: .curveEaseOut, animations: {
label.alpha = 1
}, completion: nil)
} else {
UIView.animate(withDuration: 0.6, delay: 0.3, options: .curveEaseOut, animations: {
label.alpha = 0
}, completion: nil)
}
}
}
}
}
class MinimalisticTableView: UIScrollView {
var items = [String]()
init(frame:CGRect, items:[String]) {
super.init(frame: frame)
self.items = items
self.setup(width:frame.size.width)
self.delegate = self
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setup(width:CGFloat) {
let height = items.count*120
self.contentSize = CGSize(width:width,height:CGFloat(height))
// create views
for i in 1...items.count {
let animationView = AnimationView(frame: CGRect(x: width-animateWidth, y: CGFloat(i*120)-40, width: animateWidth, height: 2))
animationView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.6)
self.addSubview(animationView)
let subGoalLabel = UILabel(frame: CGRect(x: width-animateWidth, y: CGFloat(i*120)-35-40, width: animateWidth, height: 30))
subGoalLabel.textColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
subGoalLabel.text = items[i-1]
self.addSubview(subGoalLabel)
let button = UIButton(frame: CGRect(x: width-animateWidth-44-16, y: CGFloat(i*120)-40-22, width: 44, height: 44))
button.setImage(UIImage.Asset.PlusIcon.image, for: .normal)
button.addTarget(self, action: #selector(SexyScrollView.checkMarkHandler) , for: .touchUpInside)
self.addSubview(button)
}
for view in self.subviews {
if let view = view as? AnimationView {
if view.frame.intersects(CGRect(origin: self.contentOffset, size: CGSize(width: self.frame.size.width+20, height: self.frame.size.height))) {
// the view is visible
view.frame.origin.x = (self.superview?.frame.size.width)!-animateWidth
view.frame.size.width = animateWidth
} else {
view.frame.origin.x = (self.superview?.frame.size.width)!
view.frame.size.width = 0
}
}
if let button = view as? UIButton {
if button.frame.intersects(CGRect(origin: self.contentOffset, size: CGSize(width: self.frame.size.width+20, height: self.frame.size.height))) {
// the view is visible
button.alpha = 1
} else {
button.alpha = 0
}
}
if let label = view as? UILabel {
if label.frame.intersects(CGRect(origin: self.contentOffset, size: CGSize(width: self.frame.size.width+20, height: self.frame.size.height))) {
// the view is visible
UIView.animate(withDuration: 0.6, delay: 0.3, options: .curveEaseOut, animations: {
label.alpha = 1
}, completion: nil)
} else {
UIView.animate(withDuration: 0.6, delay: 0.3, options: .curveEaseOut, animations: {
label.alpha = 0
}, completion: nil)
}
}
}
}
// MARK: Handlers
func checkMarkHandler() {
}
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
}
class AnimationView:UIView {
var isAnimating = false
func outAnimate() {
if isAnimating == true {
return
}
self.isAnimating = true
UIView.animate(withDuration: 0.6, delay: 0, options: .curveEaseOut, animations: {
self.frame.origin.x = (self.superview?.frame.size.width)!-animateWidth
self.frame.size.width = animateWidth
}, completion: { _ in self.isAnimating = false })
}
func inAnimate() {
if isAnimating == true {
return
}
self.isAnimating = true
UIView.animate(withDuration: 0.6, delay: 0, options: .curveEaseOut, animations: {
self.frame.origin.x = (self.superview?.frame.size.width)!
self.frame.size.width = 0
}, completion: { _ in self.isAnimating = false })
}
}