This repository has been archived by the owner on Nov 21, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.swift
690 lines (585 loc) · 28.4 KB
/
ViewController.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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
//
// ViewController.swift
// VineTracker
//
// Created by Connor Wybranowski on 9/3/15.
// Copyright (c) 2015 Wybro. All rights reserved.
//
import UIKit
//import JBChartView
class ViewController: UIViewController, JBLineChartViewDelegate, JBLineChartViewDataSource, UITextFieldDelegate {
let customGreenBackground = UIColor(red: 19/255, green: 183/255, blue: 121/255, alpha: 1)
let customPurpleBackground = UIColor(red: 167/255, green: 99/255, blue: 208/255, alpha: 1)
let customLightPurpleBackground = UIColor(red: 196/255, green: 141/255, blue: 228/255, alpha: 1)
var dataPoints: [Int] = [Int]()
@IBOutlet var followerLineChartView: JBLineChartView!
@IBOutlet var avatarPicImageView: UIImageView!
@IBOutlet var followerCountLabel: UILabel!
@IBOutlet var newFollowersLabel: UILabel!
@IBOutlet var userSearchField: UITextField!
@IBOutlet var userSearchRightConstraint: NSLayoutConstraint!
@IBOutlet var refreshButton: UIButton!
@IBOutlet var toggleInfoButton: UIButton!
@IBOutlet var infoViewHeightConstraint: NSLayoutConstraint!
@IBOutlet var infoView: UIView!
@IBOutlet var newFollowersFromPreviousDayLabel: UILabel!
@IBOutlet var settingsButton: UIButton!
@IBOutlet var searchButton: UIButton!
@IBOutlet var followersSettingButton: UIButton!
@IBOutlet var loopsSettingButton: UIButton!
@IBOutlet var followerSettingHorizontalConstraint: NSLayoutConstraint!
@IBOutlet var loopSettingHorizontalConstraint: NSLayoutConstraint!
@IBOutlet var settingTypeLabel: UILabel!
@IBOutlet var actionPopoverView: UIView!
@IBOutlet var shadowView: UIView!
@IBOutlet var actionPopoverImageView: UIImageView!
@IBOutlet var actionPopoverLabel: UILabel!
var isRotating = false
override func viewDidLoad() {
super.viewDidLoad()
userSearchField.delegate = self
followerLineChartView.dataSource = self
followerLineChartView.delegate = self
followerLineChartView.backgroundColor = UIColor.clearColor()
followerLineChartView.showsLineSelection = false
followerLineChartView.showsVerticalSelection = false
followerLineChartView.reloadData()
self.avatarPicImageView.layer.cornerRadius = self.avatarPicImageView.frame.size.width / 2
self.avatarPicImageView.clipsToBounds = true
infoViewHeightConstraint.constant = 0
// Setup loading view
actionPopoverView.layer.cornerRadius = 5
actionPopoverView.layer.shadowOpacity = 1
actionPopoverView.layer.shadowOffset = CGSize(width: 0, height: 2)
actionPopoverView.layer.shadowRadius = 3
checkUserSearchSettings()
checkUserDisplaySetting()
fetchNewData(UserDefaultsManager.getUserSearchSettings())
}
override func viewDidAppear(animated: Bool) {
}
override func viewWillAppear(animated: Bool) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch = touches.first as! UITouch
var point = touch.locationInView(self.view)
userSearchField.resignFirstResponder()
hideUserSearch()
if !actionPopoverView.frame.contains(point) {
hideActionPopoverView()
}
}
override func viewDidLayoutSubviews() {
UserDefaultsManager.loadUserFromCache { (savedUser) -> () in
if let displaySetting = UserDefaultsManager.getUserDisplaySetting() as String!{
if displaySetting == "followerView" {
// println("new followers from yesterday: \(savedUser.newFollowersFromPreviousDate)")
self.updateInfoViewLabel(savedUser.newFollowersFromPreviousDate)
}
else if displaySetting == "loopView" {
// println("new loops from yesterday: \(savedUser.newLoopsFromPreviousDate)")
self.updateInfoViewLabel(savedUser.newLoopsFromPreviousDate)
}
}
}
}
func numberOfLinesInLineChartView(lineChartView: JBLineChartView!) -> UInt {
return 1
}
func lineChartView(lineChartView: JBLineChartView!, numberOfVerticalValuesAtLineIndex lineIndex: UInt) -> UInt {
return UInt(dataPoints.count)
}
func lineChartView(lineChartView: JBLineChartView!, verticalValueForHorizontalIndex horizontalIndex: UInt, atLineIndex lineIndex: UInt) -> CGFloat {
return CGFloat(dataPoints[Int(horizontalIndex)] as NSNumber)
}
// func lineChartView(lineChartView: JBLineChartView!, smoothLineAtLineIndex lineIndex: UInt) -> Bool {
// return true
// }
func lineChartView(lineChartView: JBLineChartView!, verticalSelectionColorForLineAtLineIndex lineIndex: UInt) -> UIColor! {
return UIColor.redColor()
}
func lineChartView(lineChartView: JBLineChartView!, colorForLineAtLineIndex lineIndex: UInt) -> UIColor! {
return UIColor.whiteColor()
}
func lineChartView(lineChartView: JBLineChartView!, widthForLineAtLineIndex lineIndex: UInt) -> CGFloat {
return 3
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
fetchNewData(textField.text)
return true
}
func textFieldDidBeginEditing(textField: UITextField) {
refreshButton.enabled = false
}
func textFieldDidEndEditing(textField: UITextField) {
refreshButton.enabled = true
}
@IBAction func refresh(sender: AnyObject) {
println("Refreshing")
refreshAnimation()
fetchNewData(UserDefaultsManager.getUserSearchSettings())
}
func checkUserSearchSettings() {
if let searchSetting = UserDefaultsManager.getUserSearchSettings() as String! {
refreshButton.enabled = true
}
else {
refreshButton.enabled = false
}
}
func checkUserDisplaySetting() {
if let displaySetting = UserDefaultsManager.getUserDisplaySetting() as String! {
if displaySetting == "followerView" {
println("followerView display setting")
followerViewMode()
}
else if displaySetting == "loopView" {
println("loopView display setting")
loopViewMode()
}
}
// First time entering app
else {
// Set default view to followers
println("first time entering app")
UserDefaultsManager.updateUserDisplaySetting("followerView")
followerViewMode()
}
}
func followerViewMode() {
println("followerViewMode")
UserDefaultsManager.updateUserDisplaySetting("followerView")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.settingTypeLabel.text = "followers"
self.userSearchField.textColor = self.customGreenBackground
self.followersSettingButton.enabled = false
self.loopsSettingButton.enabled = true
UIView.animateWithDuration(0.2, animations: { () -> Void in
self.view.backgroundColor = self.customGreenBackground
})
UserDefaultsManager.loadUserFromCache({ (savedUser) -> () in
self.updateLabels(savedUser.followerCount, newFollowers: savedUser.newFollowers)
self.updateGraph(savedUser.followerDataPoints)
})
})
}
func loopViewMode() {
println("loopViewMode")
UserDefaultsManager.updateUserDisplaySetting("loopView")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.settingTypeLabel.text = "loops"
self.userSearchField.textColor = self.customPurpleBackground
self.loopsSettingButton.enabled = false
self.followersSettingButton.enabled = true
UIView.animateWithDuration(0.2, animations: { () -> Void in
self.view.backgroundColor = self.customPurpleBackground
})
UserDefaultsManager.loadUserFromCache({ (savedUser) -> () in
self.updateLabels(savedUser.loopCount, newFollowers: savedUser.newLoops)
self.updateGraph(savedUser.loopDataPoints)
})
})
}
@IBAction func followerSettingAction(sender: UIButton) {
followerViewMode()
}
@IBAction func loopSettingAction(sender: UIButton) {
loopViewMode()
}
func fetchNewData(searchString: String?) {
println("Fetching data")
if (searchString != nil) {
if !searchString!.isEmpty {
showActionPopoverView("loading")
VineConnection.getUserDataForName(searchString!, completionHandler: { (vineUser:VineUser, error:String) -> () in
if !error.isEmpty {
println("error: \(error)")
self.stopSpinningAction()
self.showActionPopoverView("noUser")
return
}
else {
self.hideActionPopoverView()
println("search successful - saving search")
UserDefaultsManager.updateUserSearchSettings(searchString!)
}
self.hideUserSearch()
// Update settings
var foundUser: SavedUser? = nil
var newFollowers = 0
var newFollowersFromPreviousDate = 0
var newLoops = 0
var newLoopsFromPreviousDate = 0
UserDefaultsManager.getSavedUser("\(vineUser.userId)", completionHandler: { (savedUser) -> () in
println("saved user: \(savedUser)")
foundUser = savedUser
})
if foundUser != nil {
var followerDataPoints = foundUser!.followerDataPoints
followerDataPoints?.append(vineUser.followerCount)
if followerDataPoints?.endIndex > 20 {
followerDataPoints?.removeAtIndex(0)
}
var loopDataPoints = foundUser!.loopDataPoints
loopDataPoints?.append(vineUser.loopCount)
if loopDataPoints?.endIndex > 20 {
loopDataPoints?.removeAtIndex(0)
}
if let displaySetting = UserDefaultsManager.getUserDisplaySetting() as String!{
if displaySetting == "followerView" {
self.updateGraph(followerDataPoints)
}
else if displaySetting == "loopView" {
self.updateGraph(loopDataPoints)
}
}
let calendar = NSCalendar.currentCalendar()
var startingFollowers = foundUser!.startingFollowers
var startingLoops = foundUser!.startingLoops
newFollowersFromPreviousDate = foundUser!.newFollowersFromPreviousDate!
newLoopsFromPreviousDate = foundUser!.newLoopsFromPreviousDate!
if !calendar.isDateInToday(foundUser!.date) {
// Data is one day old
if calendar.isDateInYesterday(foundUser!.date) {
// Update newLoops/Follower data
newFollowersFromPreviousDate = vineUser.followerCount - startingFollowers
newLoopsFromPreviousDate = vineUser.loopCount - startingLoops
}
// Data is older than one day
else {
// Reset values to 0
newFollowersFromPreviousDate = 0
newLoopsFromPreviousDate = 0
}
// Reset starting values
startingFollowers = vineUser.followerCount
startingLoops = vineUser.loopCount
}
newFollowers = vineUser.followerCount - startingFollowers
newLoops = vineUser.loopCount - startingLoops
var now = NSDate()
// var testDate = NSDate(timeInterval: -86500, sinceDate: now) // TEST - REMOVE
// println("TEST DATE: \(testDate)") // TEST - REMOVE
// println("Starting Loops: \(startingLoops)") // TEST - REMOVE
// println("New Loops: \(newLoops)") // TEST - REMOVE
// println("Starting Followers: \(startingFollowers)") // TEST - REMOVE
// println("New Followers: \(newFollowers)") // TEST - REMOVE
let userToSave = SavedUser(username: vineUser.username, userId: vineUser.userId, avatarPic: vineUser.avatarPic, followerCount: vineUser.followerCount, newFollowers: newFollowers, followerDataPoints: followerDataPoints, loopCount: vineUser.loopCount, newLoops: newLoops, loopDataPoints: loopDataPoints, date: now, startingFollowers: startingFollowers, newFollowersFromPreviousDate: newFollowersFromPreviousDate, startingLoops: startingLoops, newLoopsFromPreviousDate: newLoopsFromPreviousDate)
UserDefaultsManager.saveUser(userToSave, key: "\(vineUser.userId)")
UserDefaultsManager.saveUserToCache(userToSave)
println("User Found")
println(userToSave)
}
else {
println("User not found -- creating new record")
var now = NSDate()
var newUser = SavedUser(username: vineUser.username, userId: vineUser.userId, avatarPic: vineUser.avatarPic, followerCount: vineUser.followerCount, newFollowers: newFollowersFromPreviousDate, followerDataPoints: [vineUser.followerCount], loopCount: vineUser.loopCount, newLoops: newLoops, loopDataPoints: [vineUser.loopCount], date: now, startingFollowers: vineUser.followerCount, newFollowersFromPreviousDate: 0, startingLoops: vineUser.loopCount, newLoopsFromPreviousDate: 0)
UserDefaultsManager.saveUser(newUser, key: "\(vineUser.userId)")
UserDefaultsManager.saveUserToCache(newUser)
if let displaySetting = UserDefaultsManager.getUserDisplaySetting() as String!{
if displaySetting == "followerView" {
self.updateGraph([vineUser.followerCount])
}
else if displaySetting == "loopView" {
self.updateGraph([vineUser.loopCount])
}
}
}
// Use separate UI update function here
if let displaySetting = UserDefaultsManager.getUserDisplaySetting() as String!{
if displaySetting == "followerView" {
self.updateLabels(vineUser.followerCount, newFollowers: newFollowers)
}
else if displaySetting == "loopView" {
self.updateLabels(vineUser.loopCount, newFollowers: newLoops)
}
}
// self.updateLabels(vineUser.followerCount, newFollowers: newFollowers)
self.updateAvatarPic(vineUser.avatarPic)
})
}
}
}
func updateLabels(followers: Int, newFollowers: Int) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
var followersFormatted = NSNumberFormatter.localizedStringFromNumber(followers, numberStyle: NSNumberFormatterStyle.DecimalStyle)
var newFollowersFormatted = NSNumberFormatter.localizedStringFromNumber(newFollowers, numberStyle: NSNumberFormatterStyle.DecimalStyle)
self.followerCountLabel.text = followersFormatted
if newFollowers >= 0 {
self.newFollowersLabel.text = "+\(newFollowersFormatted)"
}
else if newFollowers < 0 {
self.newFollowersLabel.text = "\(newFollowersFormatted)"
}
})
}
func updateInfoViewLabel(newFollowers: Int) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
var newFollowersFormatted = NSNumberFormatter.localizedStringFromNumber(newFollowers, numberStyle: NSNumberFormatterStyle.DecimalStyle)
if newFollowers >= 0 {
self.newFollowersFromPreviousDayLabel.text = "+\(newFollowersFormatted)"
}
else if newFollowers < 0 {
self.newFollowersFromPreviousDayLabel.text = "\(newFollowersFormatted)"
}
})
}
func updateAvatarPic(image: UIImage) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.avatarPicImageView.image = image
})
}
func updateGraph(dataPointsArr: [Int]) {
dataPoints.removeAll(keepCapacity: false)
println("Updating graph")
println(dataPointsArr)
for entry in dataPointsArr {
dataPoints.append(Int(entry as NSNumber))
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.followerLineChartView.reloadData()
})
}
@IBAction func searchAction(sender: UIButton) {
if sender.tag == 0 {
showUserSearch()
hideFollowerSettingButton()
hideLoopSettingButton()
userSearchField.becomeFirstResponder()
}
else {
hideUserSearch()
}
}
@IBAction func settingsAction(sender: UIButton) {
if sender.tag == 0 {
showFollowerSettingButton()
showLoopSettingButton()
// sender.tag = 1
}
else {
hideFollowerSettingButton()
hideLoopSettingButton()
sender.tag = 0
}
}
func showFollowerSettingButton() {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.settingsButton.tag = 1
self.followersSettingButton.hidden = false
self.followersSettingButton.alpha = 0
self.followerSettingHorizontalConstraint.constant = 0
self.view.layoutIfNeeded()
self.followerSettingHorizontalConstraint.constant = 14
UIView.animateWithDuration(0.2, delay: 0, options: .CurveEaseOut, animations: { () -> Void in
self.view.layoutIfNeeded()
self.followersSettingButton.alpha = 1
}) { (completed) -> Void in
}
})
}
func hideFollowerSettingButton() {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.settingsButton.tag = 0
self.followerSettingHorizontalConstraint.constant = 0
UIView.animateWithDuration(0.2, delay: 0, options: .CurveEaseOut, animations: { () -> Void in
self.view.layoutIfNeeded()
self.followersSettingButton.alpha = 0
}) { (completed) -> Void in
self.followersSettingButton.hidden = true
}
})
}
func showLoopSettingButton() {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.loopsSettingButton.hidden = false
self.loopsSettingButton.alpha = 0
self.loopSettingHorizontalConstraint.constant = 0
self.view.layoutIfNeeded()
self.loopSettingHorizontalConstraint.constant = 8
UIView.animateWithDuration(0.2, delay: 0.1, options: .CurveEaseOut, animations: { () -> Void in
self.view.layoutIfNeeded()
self.loopsSettingButton.alpha = 1
}) { (completed) -> Void in
}
})
}
func hideLoopSettingButton() {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.loopSettingHorizontalConstraint.constant = 0
UIView.animateWithDuration(0.2, delay: 0, options: .CurveEaseOut, animations: { () -> Void in
self.view.layoutIfNeeded()
self.loopsSettingButton.alpha = 0
}) { (completed) -> Void in
self.loopsSettingButton.hidden = true
}
})
}
func showUserSearch() {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.settingsButton.hidden = true
self.userSearchField.enabled = true
self.userSearchField.alpha = 0
self.userSearchField.hidden = false
self.userSearchRightConstraint.constant = 439
self.view.layoutIfNeeded()
self.userSearchRightConstraint.constant = 8
UIView.animateWithDuration(0.4, delay: 0, options: .CurveEaseOut, animations: { () -> Void in
self.view.layoutIfNeeded()
self.userSearchField.alpha = 1
}) { (completed) -> Void in
self.searchButton.tag = 1
self.userSearchField.becomeFirstResponder()
}
})
}
func hideUserSearch() {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.userSearchField.enabled = false
self.userSearchRightConstraint.constant = 439
UIView.animateWithDuration(0.4, delay: 0, options: .CurveEaseOut, animations: { () -> Void in
self.view.layoutIfNeeded()
self.userSearchField.alpha = 0
}) { (completed) -> Void in
// make fade animation for this
self.settingsButton.hidden = false
self.userSearchField.text = ""
self.userSearchField.hidden = true
self.searchButton.tag = 0
}
})
}
func refreshAnimation() {
UIView.animateWithDuration(0.3, animations: { () -> Void in
let transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
self.refreshButton.transform = transform
}) { (completed) -> Void in
self.closingRefreshAnimation()
}
}
func closingRefreshAnimation() {
UIView.animateWithDuration(0.3, animations: { () -> Void in
let transform = CGAffineTransformMakeRotation(0)
self.refreshButton.transform = transform
}) { (completed) -> Void in
//
}
}
@IBAction func toggleInfo(sender: UIButton) {
// Hidden - show view
if sender.tag == 0 {
showMoreInfo()
sender.tag = 1
}
// Visible - hide view
else {
hideMoreInfo()
sender.tag = 0
}
}
func showMoreInfo() {
infoView.alpha = 0
infoView.hidden = false
infoViewHeightConstraint.constant = 80
UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseOut, animations: { () -> Void in
self.view.layoutIfNeeded()
self.infoView.alpha = 1
let transform = CGAffineTransformMakeRotation(CGFloat(180.0 * M_PI/180.0))
self.toggleInfoButton.transform = transform
}) { (completed) -> Void in
//
}
}
func hideMoreInfo() {
infoViewHeightConstraint.constant = 60
UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseOut, animations: { () -> Void in
self.view.layoutIfNeeded()
self.infoView.alpha = 0
let transform = CGAffineTransformMakeRotation(0)
self.toggleInfoButton.transform = transform
}) { (completed) -> Void in
self.infoView.hidden = true
}
}
//MARK: ActionPopoverView Methods
func showActionPopoverView(type: String) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
switch type {
case "loading":
self.actionPopoverImageView.image = UIImage(named: "loadingImage")
self.actionPopoverLabel.text = "loading..."
self.spinningAction()
case "error":
self.actionPopoverImageView.image = UIImage(named: "errorImage")
self.actionPopoverLabel.text = "Something went wrong"
case "noUser":
self.actionPopoverImageView.image = UIImage(named: "errorImage")
self.actionPopoverLabel.text = "User not found"
default:
break
}
self.animateActionPopoverIn()
})
}
func hideActionPopoverView() {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.animateActionPopoverOut()
})
}
func animateActionPopoverIn() {
actionPopoverView.transform = CGAffineTransformMakeScale(0.8, 0.8)
actionPopoverView.alpha = 0
shadowView.alpha = 0
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .CurveEaseInOut, animations: { () -> Void in
self.actionPopoverView.transform = CGAffineTransformMakeScale(1, 1)
self.actionPopoverView.alpha = 1
self.shadowView.alpha = 0.3
}) { (completed) -> Void in
}
}
func animateActionPopoverOut() {
UIView.animateWithDuration(0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .CurveEaseInOut, animations: { () -> Void in
self.actionPopoverView.transform = CGAffineTransformMakeScale(0.8, 0.8)
self.actionPopoverView.alpha = 0
self.shadowView.alpha = 0
}) { (completed) -> Void in
self.stopSpinningAction()
}
}
func spinningAction() {
// check if it is not rotating
if !isRotating {
// create a spin animation
let spinAnimation = CABasicAnimation()
// starts from 0
spinAnimation.fromValue = 0
// goes to 360 ( 2 * π )
spinAnimation.toValue = M_PI*2
// define how long it will take to complete a 360
spinAnimation.duration = 1
// make it spin infinitely
spinAnimation.repeatCount = Float.infinity
// do not remove when completed
spinAnimation.removedOnCompletion = false
// specify the fill mode
spinAnimation.fillMode = kCAFillModeForwards
// and the animation acceleration
spinAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
// add the animation to the button layer
actionPopoverImageView.layer.addAnimation(spinAnimation, forKey: "transform.rotation.z")
} else {
// remove the animation
actionPopoverImageView.layer.removeAllAnimations()
isRotating = false
}
}
func stopSpinningAction() {
isRotating = true
spinningAction()
}
}