-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameLauncherViewController.swift
97 lines (78 loc) · 3.24 KB
/
GameLauncherViewController.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
//
// GameLauncherViewController.swift
// Charades
//
// Created by Jeff on 9/3/15.
// Copyright © 2015 Jeff Berman. All rights reserved.
//
import UIKit
import AVFoundation
struct LauncherConstants {
static let backgroundMusicLoops = 1
}
class GameLauncherViewController: UIViewController {
@IBOutlet weak var flippyText: UIImageView!
@IBOutlet weak var wordsText: UIImageView!
var selectedCategories: [String]?
var backgroundMusic: AVAudioPlayer?
var wordTimer = NSTimer()
var wordTracker = 0
override func viewDidLoad() {
super.viewDidLoad()
if let soundURL = NSBundle.mainBundle().URLForResource("snappy_lo", withExtension: "mp3") {
backgroundMusic = try? AVAudioPlayer(contentsOfURL: soundURL)
backgroundMusic?.numberOfLoops = LauncherConstants.backgroundMusicLoops
backgroundMusic?.play()
}
// Set up game defaults
if NSUserDefaults.standardUserDefaults().integerForKey(SettingsConstants.gameDurationKey) == 0 {
NSUserDefaults.standardUserDefaults().setInteger(60, forKey: SettingsConstants.gameDurationKey)
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: false)
wordTimer = createFlipTimer()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if wordTimer.valid { wordTimer.invalidate() }
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Create timer that controls the title text's flip animations
func createFlipTimer() -> NSTimer {
let t = Int(arc4random_uniform(10)) + 5
let timer = NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(t), target: self, selector: "flipTitleWords:", userInfo: wordTracker, repeats: false)
return timer
}
// Animate the title text's flipping animation
func flipTitleWords(timer: NSTimer) {
print("GameLauncher.flipTitleWords called")
let image = self.wordTracker == 0 ? self.flippyText : self.wordsText
UIView.animateWithDuration(0.35, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut,
animations: {
image.transform = CGAffineTransformMakeScale(1.0, -1.0)
},
completion: { (completion: Bool) in
UIView.animateWithDuration(0.35, delay: 0.15, options: UIViewAnimationOptions.CurveEaseInOut,
animations: {
image.transform = CGAffineTransformMakeScale(1.0, 1.0)
},
completion: { (completion: Bool) in
self.wordTimer = self.createFlipTimer()
})
})
++wordTracker
if wordTracker > 1 { wordTracker = 0 }
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "CategoryPickerSegue" {
backgroundMusic?.stop()
if wordTimer.valid { wordTimer.invalidate() }
}
}
}