Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.