-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailboxViewController.swift
218 lines (200 loc) · 10.3 KB
/
MailboxViewController.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
//
// MailboxViewController.swift
// Mailbox
//
// Created by Marc Haumann on 2/20/16.
// Copyright © 2016 Marc Haumann. All rights reserved.
//
import UIKit
class MailboxViewController: UIViewController {
@IBOutlet weak var navView: UIView!
@IBOutlet weak var tabControl: UISegmentedControl!
@IBOutlet weak var tabContainer: UIView!
@IBOutlet weak var snoozedSectionView: UIView!
@IBOutlet weak var archiveSectionView: UIView!
@IBOutlet weak var viewsContainer: UIView!
@IBOutlet weak var listView: UIView!
@IBOutlet weak var snoozeView: UIView!
@IBOutlet weak var feedView: UIImageView!
@IBOutlet weak var iconArchiveView: UIImageView!
@IBOutlet weak var iconLaterView: UIImageView!
@IBOutlet weak var messageView: UIImageView!
@IBOutlet weak var messageContainer: UIView!
@IBOutlet weak var scrollView: UIScrollView!
var screenEdgeRecognizer: UIScreenEdgePanGestureRecognizer!
var messageState: String!
var blue: UIColor! = UIColor(red: 81/255, green: 185/255, blue: 219/255, alpha: 1)
var green: UIColor! = UIColor(red: 99/255, green: 218/255, blue: 98/255, alpha: 1)
var red: UIColor! = UIColor(red: 235/255, green: 84/255, blue: 51/255, alpha: 1)
var yellow: UIColor! = UIColor(red: 250/255, green: 211/255, blue: 50/255, alpha: 1)
var brown: UIColor! = UIColor(red: 216/255, green: 166/255, blue: 117/255, alpha: 1)
override func viewDidLoad() {
super.viewDidLoad()
scrollView.contentSize = CGSize(width: 320, height: 1313)
snoozeView.alpha = 0
listView.alpha = 0
screenEdgeRecognizer = UIScreenEdgePanGestureRecognizer(target: self, action: "moveMenu:")
screenEdgeRecognizer.edges = .Left
view.addGestureRecognizer(screenEdgeRecognizer)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func canBecomeFirstResponder() -> Bool {
return true
}
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
if event?.subtype == UIEventSubtype.MotionShake{
UIView.animateWithDuration(0.4, delay: 0, options: [], animations: { () -> Void in
self.feedView.transform = CGAffineTransformMakeTranslation(0, 84)
self.messageContainer.frame.size.height = 84
self.messageView.frame.origin.x = 0
self.iconArchiveView.frame.origin.x = 19
self.iconLaterView.frame.origin.x = 278
}, completion: nil)
}
}
func moveMenu(sender: UIScreenEdgePanGestureRecognizer) {
let translation = sender.translationInView(view)
let velocity = sender.velocityInView(view)
if sender.state == UIGestureRecognizerState.Began {
// print("began")
} else if sender.state == UIGestureRecognizerState.Changed {
self.viewsContainer.frame.origin.x = translation.x
} else if sender.state == UIGestureRecognizerState.Ended {
// print(velocity)
if velocity.x > 0 {
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.2, options: [], animations: { () -> Void in
self.viewsContainer.frame.origin.x = 290
}, completion: nil)
} else if velocity.x < 0 {
UIView.animateWithDuration(0.2, delay: 0, options: [.CurveEaseOut], animations: { () -> Void in
self.viewsContainer.frame.origin.x = 0
}, completion: nil)
}
}
}
@IBAction func messageDidPan(sender: UIPanGestureRecognizer) {
let translation = sender.translationInView(view)
messageView.frame.origin.x = translation.x
if sender.state == UIGestureRecognizerState.Began {
} else if sender.state == UIGestureRecognizerState.Changed {
iconArchiveView.alpha = convertValue(translation.x, r1Min: 0, r1Max: 60, r2Min: 0, r2Max: 1)
iconLaterView.alpha = convertValue(translation.x, r1Min: 0, r1Max: -60, r2Min: 0, r2Max: 1)
if 60 ... 240 ~= translation.x {
messageContainer.backgroundColor = green
iconArchiveView.image = UIImage(named: "archive_icon")
iconArchiveView.frame.origin.x = translation.x - 40
messageState = "archive"
} else if -240 ... -60 ~= translation.x {
messageContainer.backgroundColor = yellow
iconLaterView.image = UIImage(named: "later_icon")
iconLaterView.frame.origin.x = translation.x + 340
messageState = "later"
} else if translation.x > 240 {
messageContainer.backgroundColor = red
iconArchiveView.image = UIImage(named: "delete_icon")
iconArchiveView.frame.origin.x = translation.x - 40
messageState = "delete"
} else if translation.x < -240 {
messageContainer.backgroundColor = brown
iconLaterView.image = UIImage(named: "list_icon")
iconLaterView.frame.origin.x = translation.x + 340
messageState = "list"
} else {
messageContainer.backgroundColor = UIColor.grayColor()
messageState = "release"
}
} else if sender.state == UIGestureRecognizerState.Ended {
switch messageState {
case "archive": UIView.animateWithDuration(0.3, delay: 0, options: [], animations: { () -> Void in
self.messageView.frame.origin.x = 320
self.iconArchiveView.frame.origin.x = 320-25-19
}, completion: {(Bool) -> Void in
self.collapseView()
})
case "later": UIView.animateWithDuration(0.3, delay: 0, options: [], animations: { () -> Void in
self.messageView.frame.origin.x = -320
self.iconLaterView.frame.origin.x = 19
}, completion: {(Bool) -> Void in
UIView.animateWithDuration(0.4, delay: 0, options: [], animations: { () -> Void in
self.snoozeView.alpha = 1
}, completion: nil)
})
case "delete": UIView.animateWithDuration(0.2, delay: 0, options: [], animations: { () -> Void in
self.messageView.frame.origin.x = 320
self.iconArchiveView.frame.origin.x = 320-25-19
}, completion: {(Bool) -> Void in
self.collapseView()
})
case "list": UIView.animateWithDuration(0.2, delay: 0, options: [], animations: { () -> Void in
self.messageView.frame.origin.x = -320
self.iconLaterView.frame.origin.x = 19
}, completion: {(Bool) -> Void in
UIView.animateWithDuration(0.4, delay: 0, options: [], animations: { () -> Void in
self.listView.alpha = 1
}, completion: nil)
})
default: UIView.animateWithDuration(0.3, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: [], animations: { () -> Void in
self.messageView.frame.origin.x = 0
}, completion: nil)
}
}
}
@IBAction func listDidTap(sender: AnyObject) {
UIView.animateWithDuration(0.4, delay: 0, options: [], animations: { () -> Void in
self.listView.alpha = 0
}, completion: {(Bool) -> Void in
self.collapseView()
})
}
@IBAction func snoozeDidTap(sender: AnyObject) {
UIView.animateWithDuration(0.4, delay: 0, options: [], animations: { () -> Void in
self.snoozeView.alpha = 0
}, completion: {(Bool) -> Void in
self.collapseView()
})
}
func collapseView() {
UIView.animateWithDuration(0.4, delay: 0, options: [], animations: { () -> Void in
self.iconArchiveView.alpha = 0
self.iconLaterView.alpha = 0
self.feedView.transform = CGAffineTransformMakeTranslation(0, -84)
// self.messageContainer.transform = CGAffineTransformMakeTranslation(0, -42)
self.messageContainer.frame.size.height = 0
// self.messageContainer.transform = CGAffineTransformScale(self.messageContainer.transform, 1, 0.00000000001)
}, completion: nil)
}
@IBAction func tabValueChanged(sender: UISegmentedControl) {
let index = sender.selectedSegmentIndex
if index == 0 {
self.tabControl.tintColor = yellow
UIView.animateWithDuration(0.3, delay: 0, options: [], animations: { () -> Void in
self.tabContainer.transform = CGAffineTransformMakeTranslation(320, 0)
}, completion: nil)
} else if index == 1 {
self.tabControl.tintColor = blue
UIView.animateWithDuration(0.3, delay: 0, options: [], animations: { () -> Void in
self.tabContainer.transform = CGAffineTransformIdentity
}, completion: nil)
} else if index == 2 {
self.tabControl.tintColor = green
UIView.animateWithDuration(0.3, delay: 0, options: [], animations: { () -> Void in
self.tabContainer.transform = CGAffineTransformMakeTranslation(-320, 0)
}, completion: nil)
}
}
@IBAction func menuDidTap(sender: AnyObject) {
if self.viewsContainer.frame.origin.x == 0 {
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.2, options: [], animations: { () -> Void in
self.viewsContainer.frame.origin.x = 290
}, completion: nil)
} else {
UIView.animateWithDuration(0.2, delay: 0, options: [.CurveEaseOut], animations: { () -> Void in
self.viewsContainer.frame.origin.x = 0
}, completion: nil)
}
}
}