Created
October 19, 2014 18:11
-
-
Save TimothyBJacobs/58d52234febc31c1298b to your computer and use it in GitHub Desktop.
Swift Polymorphism Tutorial LionCubs
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
// | |
// ViewController.swift | |
// LionsAndTigers | |
// | |
// Created by Timothy Jacobs on 10/18/14. | |
// Copyright (c) 2014 Iron Bound Designs. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
// background image | |
@IBOutlet weak var myImageView: UIImageView! | |
// labels | |
@IBOutlet weak var nameLabel: UILabel! | |
@IBOutlet weak var ageLabel: UILabel! | |
@IBOutlet weak var breedLabel: UILabel! | |
@IBOutlet weak var factLabel: UILabel! | |
// holds the tigers in this project | |
var tigers:[Tiger] = [] | |
// hold the lions in this project | |
var lions:[Lion] = [] | |
var currentAnimal = (species: "Tiger", index: 0) | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
var firstTiger = Tiger(age: 3, name: "Tigger", breed: "Bengal", | |
image: UIImage(named: "BengalTiger.jpg")!) | |
var secondTiger = Tiger(age: 2, name: "Tigress", breed: "Indochinese Tiger", | |
image: UIImage(named: "IndochineseTiger.jpg")!) | |
var thirdTiger = Tiger(age: 4, name: "Jacob", breed: "Malayan Tiger", | |
image: UIImage(named: "MalayanTiger.jpg")!) | |
var fourthTiger = Tiger(age: 5, name: "Spar", breed: "Siberian Tiger", | |
image: UIImage(named: "SiberianTiger.jpg")!) | |
tigers += [firstTiger, secondTiger, thirdTiger, fourthTiger] | |
loadTigerToView(firstTiger) | |
var lion = Lion(age: 4, isAlphaMale: true, | |
image: UIImage(named: "Lion.jpg")!, | |
name: "Mufasa", subspecies: "West African") | |
var lioness = Lion(age: 3, isAlphaMale: false, | |
image: UIImage(named: "Lioness.jpeg")!, | |
name: "Sarabi", subspecies: "Barbary") | |
lions += [lion, lioness] | |
var lionCub1 = LionCub(age: 1, isAlphaMale: true, | |
image: UIImage(named: "LionCub1.jpg")!, | |
name: "Simba", subspecies: "Masai") | |
var lionCub2 = LionCub(age: 1, isAlphaMale: false, | |
image: UIImage(named:"LionCub2.jpeg")!, | |
name: "Nala", subspecies: "Transvaal") | |
lions += [lionCub1, lionCub2] | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
@IBAction func nextBarButtonItemPressed(sender: UIBarButtonItem) { | |
// advance to the next animal, switching between lions, and tigers | |
updateAnimal() | |
updateView() | |
} | |
private func updateAnimal() { | |
// switch species, then grab a random animal of that species | |
switch currentAnimal { | |
case ("Tiger", _): | |
let randIndex = Int(arc4random_uniform(UInt32(lions.count))) | |
currentAnimal = ("Lion", randIndex) | |
default: | |
let randIndex = Int(arc4random_uniform(UInt32(tigers.count))) | |
currentAnimal = ("Tiger", randIndex) | |
} | |
} | |
private func updateView() { | |
if currentAnimal.species == "Tiger" { | |
loadTigerToView(tigers[currentAnimal.index]) | |
} else if currentAnimal.species == "Lion" { | |
loadLionToView(lions[currentAnimal.index]) | |
} | |
} | |
private func loadTigerToView(tiger: Tiger) { | |
// display a tiger's info on the screen | |
UIView.transitionWithView(self.view, duration: 2, | |
options: UIViewAnimationOptions.TransitionCrossDissolve, | |
animations: { | |
self.myImageView.image = tiger.image | |
self.nameLabel.text = tiger.name | |
self.ageLabel.text = "\(tiger.age)" | |
self.breedLabel.text = tiger.breed | |
self.factLabel.text = tiger.randomFact() | |
}, completion: { | |
(finished: Bool) -> () in | |
}) | |
} | |
private func loadLionToView(lion: Lion) { | |
// display a lion's info on the screen | |
UIView.transitionWithView(self.view, duration: 2, | |
options: UIViewAnimationOptions.TransitionCrossDissolve, | |
animations: { | |
self.myImageView.image = lion.image | |
self.nameLabel.text = lion.name | |
self.ageLabel.text = "\(lion.age)" | |
self.breedLabel.text = lion.subspecies | |
self.factLabel.text = lion.randomFact() | |
}, completion: { | |
(finished: Bool) -> () in | |
}) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment