This is an example code again for an exercise of the same class.
The main goal is to create a baking calculator.
It teaches you how to use some very important and basic functions like:
- using a slider
- how to convert time units and present them properly
- basic calculations
- basic user interface
Here are a few pictures of the app:
Here is the actual code:
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 |
// // ViewController.swift // Turkey Calculator // // Created by Razvigor Andreev on 10/15/14. // Copyright (c) 2014 Razvigor Andreev. All rights reserved. // import UIKit class ViewController: UIViewController { //sliders @IBOutlet weak var slider: UISlider! @IBAction func sliderMoved(sender: AnyObject) { update() } //labels @IBOutlet weak var weightLabel: UILabel! @IBOutlet weak var thawLabel: UILabel! @IBOutlet weak var bakeLabel: UILabel! @IBOutlet weak var peopleLabel: UILabel! // iVars let thawFactor = 90.0 // minutes per LB for thawing let bakeFactor = 30.0 // min per LB var people = Int(1) var weight = 1.0 var time = 1.0 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = UIColor(patternImage: UIImage(named: "texture1.png")) update () } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func update () { people = Int(slider.value) peopleLabel.text = "(people)" weight = Double(Double(people) * 0.8) time = (weight * Double(people)) calcWeight() calcTime() bakeTime() } func calcWeight () { weightLabel.text = ("(Double(people) * 0.8) lb") } func calcTime () { let thawTime = (thawFactor * weight) let thawHours = floor(thawTime/60) let thawMinutes = round(thawTime - thawHours * 60) thawLabel.text = ("(thawHours) hrs, (thawMinutes) min") } func bakeTime () { let timeBake = (bakeFactor * weight) let hoursBake = floor(timeBake/60) let minutesBake = round(timeBake - hoursBake*60) bakeLabel.text = ("(hoursBake) hrs, (minutesBake) min") } } |