Last active
August 29, 2015 14:18
-
-
Save solomon23/c644c40cf9b2f1dc58b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import AudioToolbox | |
import AVFoundation | |
class ViewController: UIViewController { | |
//Properties | |
@IBOutlet weak var countLabel: UILabel! | |
@IBOutlet var countView: UIView! | |
@IBOutlet var minusButton: UIButton! | |
@IBOutlet var plusButton: UIButton! | |
@IBOutlet var resetButton: UIButton! | |
//Sound Variables | |
var plusSound = AVAudioPlayer() | |
var plusSoundLong = AVAudioPlayer() | |
var minusSound = AVAudioPlayer() | |
var minusSoundLong = AVAudioPlayer() | |
var resetSound = AVAudioPlayer() | |
var count = 0 | |
//---------------------------------- | |
//Initial setup | |
//---------------------------------- | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
initialSetup() | |
//Button style | |
minusButton.layer.cornerRadius = 8 | |
plusButton.layer.cornerRadius = 8 | |
resetButton.layer.cornerRadius = 8 | |
//Setup audio files | |
minusSound = setupAudioPlayerWithFile("minusSound", type:"wav") | |
minusSoundLong = setupAudioPlayerWithFile("minusSoundLong", type:"wav") | |
plusSound = setupAudioPlayerWithFile("plusSound", type:"wav") | |
plusSoundLong = setupAudioPlayerWithFile("plusSoundLong", type:"wav") | |
resetSound = setupAudioPlayerWithFile("resetSound", type:"wav") | |
} | |
//---------------------------------- | |
// bounce animates a view to indicate a hit | |
func bounce(bounceView:UIView) { | |
UIView.animateWithDuration(0.12) { | |
bounceView.transform = CGAffineTransformMakeScale(0.8, 0.8) | |
bounceView.alpha = 0.8 | |
UIView.animateWithDuration(0.12) { | |
bounceView.transform = CGAffineTransformMakeScale(1, 1) | |
bounceView.alpha = 1 | |
} | |
} | |
} | |
//---------------------------------- | |
func initialSetup() { | |
updateCount(NSUserDefaults.standardUserDefaults().integerForKey("countRestore")) | |
} | |
//---------------------------------- | |
// Updates the count and persists it in userdefaults | |
func updateCount(count: Int) { | |
self.count = count | |
self.countLabel.text = "\(count)" | |
NSUserDefaults.standardUserDefaults().setInteger(count, forKey: "countRestore") | |
NSUserDefaults.standardUserDefaults().synchronize() | |
} | |
//Minus button | |
@IBAction func countMinus() { | |
updateCount(count-1) | |
minusSound.play() | |
} | |
//---------------------------------- | |
//Long minus | |
@IBAction func longMinus(sender: AnyObject) { | |
if sender.state == UIGestureRecognizerState.Began { | |
updateCount(count-5) | |
minusSoundLong.play() | |
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate)) | |
bounce(minusButton) | |
} | |
} | |
//---------------------------------- | |
//Plus button | |
@IBAction func countPlus() { | |
updateCount(count+1) | |
plusSound.play() | |
} | |
//---------------------------------- | |
//Long plus | |
@IBAction func longPlus(sender: AnyObject) { | |
if sender.state == UIGestureRecognizerState.Began { | |
updateCount(count+5) | |
plusSoundLong.play() | |
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate)) | |
bounce(plusButton) | |
} | |
} | |
//---------------------------------- | |
//Reset button | |
@IBAction func resetCount() { | |
updateCount(0) | |
bounce(resetButton) | |
resetSound.play() | |
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate)) | |
UIView.animateWithDuration(0.12) { | |
self.countLabel.transform = CGAffineTransformMakeScale(1.5, 1.5) | |
self.countLabel.alpha = 0.5 | |
UIView.animateWithDuration(0.12) { | |
self.countLabel.transform = CGAffineTransformMakeScale(1, 1) | |
self.countLabel.alpha = 1 | |
} | |
} | |
} | |
//---------------------------------- | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
} | |
//---------------------------------- | |
//Statusbar style | |
override func preferredStatusBarStyle() -> UIStatusBarStyle { | |
return UIStatusBarStyle.LightContent | |
} | |
//Sound helper | |
func setupAudioPlayerWithFile(file:String, type:String) -> AVAudioPlayer { | |
var path = NSBundle.mainBundle().pathForResource(file, ofType:type) | |
var url = NSURL.fileURLWithPath(path!) | |
var error: NSError? | |
var audioPlayer = AVAudioPlayer(contentsOfURL: url, error: &error) | |
return audioPlayer! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment