Last active
July 23, 2017 15:04
-
-
Save thiagolioy/78d61cd75b61825f1d86ab3ad4db8bbb 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 | |
class RegularViewController: UIViewController { | |
@IBOutlet var priceLabel: UILabel! | |
@IBOutlet var priceSlider: UISlider! | |
@IBOutlet var unitLabel: UILabel! | |
@IBOutlet var unitSlider: UISlider! | |
var lastPrice: Float = 0 | |
var lastUnit: Float = 0 | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupEvents() | |
} | |
func setupEvents() { | |
priceSlider.addTarget(self, action: #selector(priceSliderChanged), for: .valueChanged) | |
unitSlider.addTarget(self, action: #selector(unitSliderChanged), for: .valueChanged) | |
} | |
@objc | |
func priceSliderChanged(_ sender: UISlider) { | |
let formattedPrice = CurrencyFormatter.shared.format(sender.value) | |
priceLabel.text = formattedPrice | |
lastPrice = sender.value | |
notifyIfBigSell() | |
} | |
@objc | |
func unitSliderChanged(_ sender: UISlider) { | |
unitLabel.text = "\(Int(sender.value)) units" | |
lastUnit = sender.value | |
notifyIfBigSell() | |
} | |
func notifyIfBigSell() { | |
if lastPrice > 50 && lastUnit > 50 { | |
let result = lastPrice * lastUnit | |
let formattedResult = CurrencyFormatter.shared.format(result) | |
print("Big Sale! \(lastUnit) units for \(formattedResult)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment