import UIKit
class ViewDiamTabBarController: UIViewController {
@IBOutlet weak var Label1: UILabel!
@IBOutlet weak var diamTextValue: UITextField!
@IBOutlet weak var diamSliderValue: UISlider!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var altoTextValue: UITextField!
@IBOutlet weak var altoSliderValue: UISlider!
@IBOutlet weak var lockButton: UIButton!
@IBOutlet weak var label3: UILabel!
@IBOutlet weak var anchoTextValue: UITextField!
@IBOutlet weak var anchoSliderValue: UISlider!
@IBOutlet weak var label4: UILabel!
@IBOutlet weak var perimetroLabel: UILabel!
@IBOutlet weak var seccionLabel: UILabel!
@IBOutlet weak var perimRectLabel: UILabel!
@IBOutlet weak var calcularButton: UIButton!
var diametroValue1 = 0
var diametroValue2 = 0
var altoValue = 0
var anchoValue = 0
var seccionValue = 0
var perimCircValue1 = 0
var perimCircValue2 = 0
var seccCircValue1 = 0
var seccCircValue2 = 0
var perimRectValue1 = 0
var perimRectValue2 = 0
@IBAction func diamTextEntry(_ sender: UITextField) {
let diamAmount = Double(diamTextValue.text!)
let altoAmount = Double(altoTextValue.text!)
let alertMessage = "Falta introducir un parámetro !"
let alertMessage2 = "Los valores introducidos deben ser superiores a 0 !"
if (diamAmount != nil && altoAmount != nil){
if (diamAmount! > 0 && altoAmount! > 0){
let seccionValue = .pi*(diamAmount!/2)*(diamAmount!/2)
let anchoValue = Double(round(1000*(seccionValue/altoAmount!))/1000)
anchoTextValue.text = String(Int(anchoValue))
} else {
let alert = UIAlertController(title: "Error de procedimiento", message: alertMessage2, preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.destructive){ (result : UIAlertAction) -> Void in print ("Cancel button selected")
}
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default){ (result : UIAlertAction) -> Void in print ("Ok button selected")
}
alert.addAction(cancelAction)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
} else {
let alert = UIAlertController(title: "Error de procedimiento", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.destructive){ (result : UIAlertAction) -> Void in print ("Cancel button selected")
}
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default){ (result : UIAlertAction) -> Void in print ("Ok button selected")
}
alert.addAction(cancelAction)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
}
@IBAction func altoTextEntry(_ sender: UITextField) {
let diamAmount = Double(diamTextValue.text!)
let altoAmount = Double(altoTextValue.text!)
let alertMessage = "Falta introducir un parámetro !"
let alertMessage2 = "Los valores introducidos deben ser superiores a 0 !"
if (diamAmount != nil && altoAmount != nil) {
if (diamAmount! > 0 && altoAmount! > 0) {
let seccionValue = .pi*(diamAmount!/2)*(diamAmount!/2)
let anchoValue = Double(round(1000*(seccionValue/altoAmount!))/1000)
anchoTextValue.text = String(Int(anchoValue))
} else {
let alert = UIAlertController(title: "Error de procedimiento", message: alertMessage2, preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.destructive){ (result : UIAlertAction) -> Void in print ("Cancel button selected")
}
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default){ (result : UIAlertAction) -> Void in print ("Ok button selected")
}
alert.addAction(cancelAction)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
} else {
let alert = UIAlertController(title: "Error de procedimiento", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.destructive){ (result : UIAlertAction) -> Void in print ("Cancel button selected")
}
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default){ (result : UIAlertAction) -> Void in print ("Ok button selected")
}
alert.addAction(cancelAction)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
}
@IBAction func anchoTextEntry(_ sender: UITextField) {
let diamAmount = Double(diamTextValue.text!)
let anchoAmount = Double(anchoTextValue.text!)
let altoAmount = Double(altoTextValue.text!)
let alertMessage = "Falta introducir un parámetro !"
let alertMessage2 = "Los valores introducidos deben ser superiores a 0 !"
if (diamAmount != nil && anchoAmount != nil) && altoAmount != nil {
if (diamAmount! > 0 && altoAmount! > 0 && anchoAmount! > 0) {
let seccionValue = .pi*(diamAmount!/2)*(diamAmount!/2)
let altoValue = Double(round(1000*(seccionValue/anchoAmount!))/1000)
altoTextValue.text = String(Int(altoValue))
} else {
let alert = UIAlertController(title: "Error de procedimiento", message: alertMessage2, preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.destructive){ (result : UIAlertAction) -> Void in print ("Cancel button selected")
}
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default){ (result : UIAlertAction) -> Void in print ("Ok button selected")
}
alert.addAction(cancelAction)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
} else {
let alert = UIAlertController(title: "Error de procedimiento", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.destructive){ (result : UIAlertAction) -> Void in print ("Cancel button selected")
}
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default){ (result : UIAlertAction) -> Void in print ("Ok button selected")
}
alert.addAction(cancelAction)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
}
@IBAction func diamSliderMove(_ sender: UISlider) {
diamTextValue.text = String(Int(sender.value))
let diamAmount = Double(diamTextValue.text!)
let altoAmount = Double(altoTextValue.text!)
let seccionValue = .pi*(diamAmount!/2)*(diamAmount!/2)
let anchoValue = Double(round(1000*(seccionValue/altoAmount!))/1000)
anchoTextValue.text = String(Int(anchoValue))
}
@IBAction func altoSliderMove(_ sender: UISlider) {
altoTextValue.text = String(Int(sender.value))
let diamAmount = Double(diamTextValue.text!)
let altoAmount = Double(altoTextValue.text!)
let seccionValue = .pi*(diamAmount!/2)*(diamAmount!/2)
let anchoValue = Double(round(1000*(seccionValue/altoAmount!))/1000)
anchoTextValue.text = String(Int(anchoValue))
}
@IBAction func anchoSliderMove(_ sender: UISlider) {
anchoTextValue.text = String(Int(sender.value))
let altoAmount = Double(altoTextValue.text!)
let anchoAmount = Double(anchoTextValue.text!)
let diamAmount = Double(diamTextValue.text!)
if lockButton.currentBackgroundImage == imageLiteral(resourceName: "lockred25")
{
let seccionValue = (altoAmount!)*(anchoAmount!)
let diametroValue1 = 2*sqrt((seccionValue)/(.pi))
let diametroValue2 = Double(round(1000*(diametroValue1))/1000)
diamTextValue.text = String(Int(diametroValue2))
} else {
let seccionValue = .pi*(diamAmount!/2)*(diamAmount!/2)
let altoValue = Double(round(1000*(seccionValue/anchoAmount!))/1000)
altoTextValue.text = String(Int(altoValue))
}
}
@IBAction func lockButtonPressed(_ sender: UIButton) {
if lockButton.currentBackgroundImage == imageLiteral(resourceName: "lockblue25")
{
lockButton.setBackgroundImage( imageLiteral(resourceName: "lockred25"), for: .normal)
} else {
lockButton.setBackgroundImage( imageLiteral(resourceName: "lockblue25"), for: .normal)
}
}
@IBAction func calcButtonPressed(_ sender: UIButton) {
let diamAmount = Double(diamTextValue.text!)
let altoAmount = Double(altoTextValue.text!)
let anchoAmount = Double(anchoTextValue.text!)
let alertMessage = "Falta introducir un parámetro !"
if (diamAmount != nil && altoAmount != nil && anchoAmount != nil){
let perimCircValue1 = .pi*(diamAmount!)/1000
let perimCircValue2 = Double(round(1000*perimCircValue1)/1000)
let PerimCircString = String(perimCircValue2)
perimetroLabel.text = PerimCircString
let seccCircValue1 = .pi*(diamAmount!/2000)*(diamAmount!/2000)
let seccCircValue2 = Double(round(1000*seccCircValue1)/1000)
let SeccCircString = String(seccCircValue2)
seccionLabel.text = SeccCircString
let perimRectValue1 = 2*((altoAmount!/1000)+(anchoAmount!/1000))
let perimRectValue2 = Double(round(1000*perimRectValue1)/1000)
let PerimRectString = String(perimRectValue2)
perimRectLabel.text = PerimRectString
} else{
let alert = UIAlertController(title: "Error de procedimiento", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.destructive){ (result : UIAlertAction) -> Void in print ("Cancel button selected")
}
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default){ (result : UIAlertAction) -> Void in print ("Ok button selected")
}
alert.addAction(cancelAction)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
Label1.layer.borderWidth = 1.0
Label1.layer.cornerRadius = 8
Label1.layer.borderColor = UIColor.lightGray.cgColor
diamTextValue.layer.borderWidth = 0.5
diamTextValue.layer.borderColor = UIColor.gray.cgColor
diamSliderValue.thumbTintColor = UIColor.lightGray
label2.layer.borderWidth = 1.0
label2.layer.cornerRadius = 8
label2.layer.borderColor = UIColor.lightGray.cgColor
altoTextValue.layer.borderWidth = 0.5
altoTextValue.layer.borderColor = UIColor.gray.cgColor
altoSliderValue.thumbTintColor = UIColor.lightGray
label3.layer.borderWidth = 1.0
label3.layer.cornerRadius = 8
label3.layer.borderColor = UIColor.lightGray.cgColor
anchoTextValue.layer.borderWidth = 0.5
anchoTextValue.layer.borderColor = UIColor.gray.cgColor
anchoSliderValue.thumbTintColor = UIColor.lightGray
label4.layer.borderWidth = 1.0
label4.layer.cornerRadius = 8
label4.layer.borderColor = UIColor.lightGray.cgColor
perimetroLabel.layer.borderWidth = 1.0
perimetroLabel.layer.borderColor = UIColor.lightGray.cgColor
seccionLabel.layer.borderWidth = 1.0
seccionLabel.layer.borderColor = UIColor.lightGray.cgColor
perimRectLabel.layer.borderWidth = 1.0
perimRectLabel.layer.borderColor = UIColor.lightGray.cgColor
calcularButton.layer.borderWidth = 1.0
calcularButton.layer.cornerRadius = 8
calcularButton.layer.borderColor = UIColor.gray.cgColor
let toolBar = UIToolbar()
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.doneClicked))
toolBar.setItems([doneButton], animated: false)
diamTextValue.inputAccessoryView = toolBar
altoTextValue.inputAccessoryView = toolBar
anchoTextValue.inputAccessoryView = toolBar
}
@objc func doneClicked() {
view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}