-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainViewController.swift
executable file
·361 lines (322 loc) · 15.8 KB
/
MainViewController.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
//
// MainViewController.swift
// UCard
//
// Created by YunSang Lee on 2017. 6. 3..
// Copyright © 2017년 CITeam44. All rights reserved.
//
import UIKit
import AssetsLibrary
class MainViewController: UIViewController, UIGestureRecognizerDelegate, UISearchBarDelegate, BLEFinderDelegate {
enum Status {
case cardList
case personalCard
case cardTrasnmited
case cardReceived
case search
}
var status:Status = .cardList
enum GestureType {
case none
case showPersonalCard
case closePersonalCard
case transmitCard
case cancelTransmit
case takeReceivedCard
}
var gestureType: GestureType = .none
var pangesture:UIPanGestureRecognizer?
var personalCardTap: UITapGestureRecognizer?
var originTouchY:CGFloat = 0.0
var bleFinder:BLEFinder?
var bleAdvertiser:BLEAdvertiser?
@IBOutlet weak var cardListView: CardListView!
@IBOutlet weak var personalCardView: PersonalCardView!
@IBAction func searchButton(_ sender: UIButton) {
searchSlide()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// for gesture
if pangesture == nil {
pangesture = UIPanGestureRecognizer(target: self,action: #selector(MainViewController.pan(rec:)))
pangesture!.delegate = self
self.view.addGestureRecognizer(pangesture!)
}
if personalCardTap == nil {
personalCardTap = UITapGestureRecognizer(target: self,action: #selector(MainViewController.tap(rec:)))
personalCardTap!.delegate = self
self.view.addGestureRecognizer(personalCardTap!)
}
background();
}
override func viewDidAppear(_ animated: Bool) {
if(personalCardView.numberOfCards == 0) {
// There is no personal card, probably first time using the app
self.performSegue(withIdentifier: "tutorialSegue", sender: nil)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func background(){
if !UIAccessibilityIsReduceTransparencyEnabled() {
self.view.backgroundColor = UIColor.clear
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.view.insertSubview(blurEffectView, at:0)
let backgroundView = UIImageView(image: #imageLiteral(resourceName: "pp2"))
backgroundView.frame = self.view.bounds
backgroundView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.view.insertSubview(backgroundView, at:0)
}
}
// gesture
func pan(rec:UIPanGestureRecognizer){
let point = rec.location(in: self.view)
let shiftY:CGFloat = point.y - originTouchY
switch rec.state {
case .began:
switch status {
case .cardList:
gestureType = .showPersonalCard
originTouchY = point.y
case .personalCard:
if (point.y >= 350) { // dragging on cardListView
originTouchY = point.y - cardListView.bounds.height * 0.55
gestureType = .closePersonalCard
} else if(point.y >= 64 && point.y <= 260) { // dragging on personalCardView
gestureType = .transmitCard
originTouchY = point.y
}
case .cardTrasnmited:
if !(point.y >= 350) { // dragging on cardListView
originTouchY = point.y
gestureType = .cancelTransmit
}
case .cardReceived:
if(point.y >= 64 && point.y <= 260) { // dragging on personalCardView
gestureType = .takeReceivedCard
originTouchY = point.y
}
default: break
}
case .changed:
switch gestureType {
case .showPersonalCard: // cardList
cardListView.transform = CGAffineTransform.init(translationX: 0, y: shiftY)
personalCardView.transform = CGAffineTransform.init(translationX: 0, y: shiftY)
case .closePersonalCard: // personalCard
cardListView.transform = CGAffineTransform.init(translationX: 0, y: shiftY)
personalCardView.transform = CGAffineTransform.init(translationX: 0, y: shiftY)
case .transmitCard:
personalCardView.transform = CGAffineTransform.init(translationX: 0, y: shiftY + cardListView.bounds.height * 0.55)
case .cancelTransmit:
personalCardView.transform = CGAffineTransform.init(translationX: 0, y: sqrt(shiftY)*25)
case .takeReceivedCard:
personalCardView.transform = CGAffineTransform.init(translationX: 0, y: shiftY + cardListView.bounds.height * 0.55)
default: break
}
default:
switch gestureType {
case .showPersonalCard: // cardList
let activePersonalCard = (shiftY > personalCardView.frame.height/3) ? true : false
let newY = (activePersonalCard) ? cardListView.bounds.height * 0.55 : 0
UIView.animate(withDuration: 0.3, animations: {
self.cardListView.layer.transform = CATransform3DMakeTranslation(0, newY, 0)
self.personalCardView.layer.transform = CATransform3DMakeTranslation(0, newY, 0)
}, completion: nil)
if(activePersonalCard) {
cardListView.isUserInteractionEnabled = false
status = .personalCard
}
case .closePersonalCard: // personalCard
let deactivePersonalCard = (shiftY > personalCardView.frame.height*5/6) ? false : true
let newY = (deactivePersonalCard) ? 0 : cardListView.bounds.height * 0.55
if(deactivePersonalCard) {
cardListView.isUserInteractionEnabled = true
status = .cardList
UIView.animate(withDuration: 0.3, animations: {
self.cardListView.layer.transform = CATransform3DMakeTranslation(0, 0, 0)
self.personalCardView.layer.transform = CATransform3DMakeTranslation(0, -270, 0)
}, completion: nil)
} else {
UIView.animate(withDuration: 0.3, animations: {
self.cardListView.layer.transform = CATransform3DMakeTranslation(0, newY, 0)
self.personalCardView.layer.transform = CATransform3DMakeTranslation(0, newY, 0)
}, completion: nil)
}
case .transmitCard:
let transmitPersonalCard = ((-1 * shiftY) > personalCardView.frame.height*2/3) ? true : false
if(transmitPersonalCard) {
UIView.animate(withDuration: 0.3, animations: {
self.personalCardView.layer.transform = CATransform3DMakeTranslation(0, -270, 0)
}, completion: nil)
status = .cardTrasnmited
bleFinder = BLEFinder()
bleFinder?.uniqueID = personalCardView.uniqueId
bleFinder?.delegate = self
bleAdvertiser = BLEAdvertiser()
bleAdvertiser?.uniqueID = personalCardView.uniqueId
bleAdvertiser?.personalCard = personalCardView.getPersonalCard()!
} else {
UIView.animate(withDuration: 0.3, animations: {
self.personalCardView.layer.transform = CATransform3DMakeTranslation(0, self.cardListView.bounds.height * 0.55, 0)
}, completion: nil)
}
case .cancelTransmit:
let cancelTransmit = (shiftY > personalCardView.frame.height*1/3) ? true : false
if(cancelTransmit) {
UIView.animate(withDuration: 0.3, animations: {
self.personalCardView.layer.transform = CATransform3DMakeTranslation(0, self.cardListView.bounds.height * 0.55, 0)
}, completion: nil)
status = .personalCard
if(bleFinder != nil) {
bleFinder!.cleanUp()
bleFinder!.centralManager!.stopScan()
bleFinder = nil
}
if(bleAdvertiser != nil) {
bleAdvertiser!.stopAdvertising()
bleAdvertiser = nil
}
} else {
UIView.animate(withDuration: 0.3, animations: {
self.personalCardView.layer.transform = CATransform3DMakeTranslation(0, -270, 0)
}, completion: nil)
}
case .takeReceivedCard:
let rejectCard = ((-1 * shiftY) > personalCardView.frame.height*2/3) ? true : false
let receiveCard = (shiftY > personalCardView.frame.height*1/3) ? true : false
if(rejectCard) {
UIView.animate(withDuration: 0.3, animations: {
self.personalCardView.layer.transform = CATransform3DMakeTranslation(0, -270, 0)
}, completion: nil)
// get ready for next try
bleFinder = nil
bleAdvertiser = nil
status = .cardTrasnmited
bleFinder = BLEFinder()
bleFinder?.uniqueID = personalCardView.uniqueId
bleFinder?.delegate = self
bleAdvertiser = BLEAdvertiser()
bleAdvertiser?.uniqueID = personalCardView.uniqueId
bleAdvertiser?.personalCard = personalCardView.getPersonalCard()!
status = .cardTrasnmited
} else if(receiveCard) {
UIView.animate(withDuration: 0.3, animations: {
self.personalCardView.layer.transform = CATransform3DMakeTranslation(0, 700, 0)
}, completion: { finished in
UIView.animate(withDuration: 0.3, animations: {
self.cardListView.layer.transform = CATransform3DMakeTranslation(0, 0, 0)
}, completion: nil)
self.personalCardView.layer.transform = CATransform3DMakeTranslation(0, -270, 0)
self.personalCardView.loadPersonalCard()
self.status = .cardList
self.cardListView.isUserInteractionEnabled = true
})
cardListView.addCard(name: personalCardView.name, titleName: personalCardView.title, companyName: personalCardView.company, phone: personalCardView.phone, email: personalCardView.email, addressOne: personalCardView.addressOne, addressTwo: personalCardView.addressTwo, design: personalCardView.design)
} else {
UIView.animate(withDuration: 0.3, animations: {
self.personalCardView.layer.transform = CATransform3DMakeTranslation(0, self.cardListView.bounds.height * 0.55, 0)
}, completion: nil)
}
default: break
}
gestureType = .none
}
}
func tap(rec: UITapGestureRecognizer) {
if(status == .personalCard) {
let point = rec.location(in: self.view)
if (personalCardView.frame.contains(point)) {
self.performSegue(withIdentifier: "personalInfoSegue", sender: nil)
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is PersonalInfoViewController {
let destination = segue.destination as! PersonalInfoViewController
destination.mainViewController = self
} else if segue.destination is TutorialViewController {
let destination = segue.destination as! TutorialViewController
destination.mainViewController = self
}
}
open func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
let point = gestureRecognizer.location(in: self.view)
print("x: \(point.x)\t\ty: \(point.y)\n");
switch status {
case .cardList:
if (point.y > 20 && point.y < 80) && status == .cardList { // dragging on top margin
return true
}
default:
return true
}
return false
}
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return false
}
func searchSlide() {
if(status == .cardList) {
let searchBarFrame = CGRect(x: 0, y: -44, width: self.view.bounds.width, height: 44)
let searchBar = UISearchBar(frame: searchBarFrame)
searchBar.delegate = self
searchBar.setShowsCancelButton(true, animated: false)
if let cancelButton = searchBar.value(forKey: "cancelButton") as? UIButton {
cancelButton.isEnabled = true
}
self.view.addSubview(searchBar)
UIView.animate(withDuration: 0.3, delay: 0, options: [],
animations: {
searchBar.frame = CGRect(x: 0, y: 20, width: self.view.bounds.width, height: 44)
},
completion: nil
)
status = .search
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let searchText = searchBar.text as String!
print("searchText \(searchText!)")
self.cardListView.search(with: searchText!)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
UIView.animate(withDuration: 0.3, delay: 0, options: [],
animations: {
searchBar.frame = CGRect(x: 0, y: -44, width: self.view.bounds.width, height: 44)
},
completion: { _ in
searchBar.removeFromSuperview()
self.status = .cardList
}
)
}
func BLEFinderFoundClosest(key: String) {
bleAdvertiser?.targetPeripheralID = key
}
func BLEFinderVerifiedCurrentCentral() {
bleAdvertiser?.centralIsVerified = true
}
func BLEFinderHasReceivedCard(name: String, titleName: String, companyName: String, phone: String, email: String, addressOne: String, addressTwo: String, design: String) {
personalCardView.name = name
personalCardView.title = titleName
personalCardView.company = companyName
personalCardView.phone = phone
personalCardView.email = email
personalCardView.addressOne = addressOne
personalCardView.addressTwo = addressTwo
personalCardView.design = design
personalCardView.updateCard()
status = .cardReceived
UIView.animate(withDuration: 0.3, animations: {
self.personalCardView.layer.transform = CATransform3DMakeTranslation(0, self.cardListView.bounds.height * 0.55, 0)
}, completion: nil)
}
}