Skip to content

Instantly share code, notes, and snippets.

@marcboeren
Created March 19, 2015 19:53
Show Gist options
  • Save marcboeren/46749b21897fa1a90a12 to your computer and use it in GitHub Desktop.
Save marcboeren/46749b21897fa1a90a12 to your computer and use it in GitHub Desktop.
Three blinking lights (chained animated UIImageViews).
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var light1: UIImageView!
@IBOutlet weak var light2: UIImageView!
@IBOutlet weak var light3: UIImageView!
enum BlinkingStatus {
case Blinking, Off
}
var blinkingStatus: BlinkingStatus = .Blinking
override func viewDidLoad() {
super.viewDidLoad()
self.startBlinking()
}
func startBlinking() {
self.blinkingStatus = .Blinking
self.lightLight(1)
}
func stopBlinking() {
self.blinkingStatus = .Off
}
func getLight(number: Int) -> UIImageView {
switch number {
case 1: return self.light1
case 2: return self.light2
case 3: return self.light3
default: return self.light1
}
}
func lightLight(number: Int) {
var light = self.getLight(number)
UIView.animateWithDuration(1.0,
delay: 0.0,
options: nil,
animations: {
light.alpha = 1
},
completion: { finished in
self.douseLight(number)
}
)
}
func douseLight(number: Int) {
var light = self.getLight(number)
UIView.animateWithDuration(0.5,
delay: 0.0,
options: nil,
animations: {
light.alpha = 0
},
completion: { finished in
if self.blinkingStatus == .Blinking {
self.lightLight((number % 3) + 1)
}
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment