1
我在iOS 9,Swift和Xcode 7中製作計算器應用程序,我想知道如何在用戶切換到風景模式時創建UIButton。在數字左側增加更多的操作符,並且更改,將按鈕移開以騰出空間給新的操作員。 iOS的基本計算器可以做到這一點,我想知道如何?Swift - 在風景中創建UIButton
我是iOS開發新手,我一直在瀏覽網頁只找到一個覆蓋函數:viewWillTransitionToSize()
。這是做這種事情的正確功能嗎?
此外,每次進入橫向視圖時,都會創建另一個按鈕。我怎樣才能阻止這種情況發生?
附件是我的程序創建的UIButton和我上面列出的功能。
對不起,有一系列問題/問題。我很難在這個問題上找到教程或幫助。任何指導或解決方案都將有所幫助。非常感謝。
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var outputDisplay: UILabel!
@IBOutlet weak var number7: UIButton!
@IBOutlet weak var number1: UIButton!
@IBOutlet weak var number2: UIButton!
@IBOutlet weak var number3: UIButton!
@IBOutlet weak var number4: UIButton!
@IBOutlet weak var number5: UIButton!
@IBOutlet weak var number6: UIButton!
@IBOutlet weak var number9: UIButton!
@IBOutlet weak var number8: UIButton!
@IBOutlet weak var numberZero: UIButton!
@IBOutlet weak var decimalPoint: UIButton!
@IBOutlet weak var plusMinusToggle: UIButton!
@IBOutlet weak var acClear: UIButton!
@IBOutlet weak var equalButton: UIButton!
@IBOutlet weak var multiply: UIButton!
@IBOutlet weak var divide: UIButton!
@IBOutlet weak var subtraction: UIButton!
@IBOutlet weak var addition: UIButton!
@IBOutlet weak var deletion: UIButton!
@IBOutlet weak var squareRoot: UIButton!
var typing:Bool = false
var firstNumber: Float = 0.0
var secondNumber: Float = 0.0
var operation:String = ""
var result: Float = 0.0
var valueToPass: Float = 0.0
override func viewDidLoad() {
super.viewDidLoad()
number1.layer.cornerRadius = number1.bounds.size.width/8.0
number2.layer.cornerRadius = number2.bounds.size.width/8.0
number3.layer.cornerRadius = number3.bounds.size.width/8.0
number4.layer.cornerRadius = number4.bounds.size.width/8.0
number5.layer.cornerRadius = number5.bounds.size.width/8.0
number6.layer.cornerRadius = number6.bounds.size.width/8.0
number7.layer.cornerRadius = number7.bounds.size.width/8.0
number8.layer.cornerRadius = number8.bounds.size.width/8.0
number9.layer.cornerRadius = number9.bounds.size.width/8.0
numberZero.layer.cornerRadius = numberZero.bounds.size.width/8.0
equalButton.layer.cornerRadius = equalButton.bounds.size.width/8.0
deletion.layer.cornerRadius = deletion.bounds.size.width/8.0
plusMinusToggle.layer.cornerRadius = plusMinusToggle.bounds.size.width/8.0
decimalPoint.layer.cornerRadius = decimalPoint.bounds.size.width/8.0
acClear.layer.cornerRadius = acClear.bounds.size.width/8.0
addition.layer.cornerRadius = addition.bounds.size.width/8.0
divide.layer.cornerRadius = divide.bounds.size.width/8.0
multiply.layer.cornerRadius = multiply.bounds.size.width/8.0
subtraction.layer.cornerRadius = subtraction.bounds.size.width/8.0
squareRoot.layer.cornerRadius = squareRoot.bounds.size.width/8.0
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
if (UIDevice.currentDevice().orientation.isLandscape) {
print("Device in Lanscape")
let newButton1 = UIButton(type: UIButtonType.System) as UIButton
newButton1.frame = CGRectMake(100, 100, 100, 50)
newButton1.backgroundColor = UIColor.whiteColor()
newButton1.center = CGPoint(x: number1.frame.width, y: number1.frame.height)
newButton1.layer.cornerRadius = newButton1.bounds.size.width/8.0
self.view.addSubview(newButton1)
}
else{}
}
@IBAction func numberPressed(sender: AnyObject) {
// set the variable "number" to whichever number the user presses
let number = sender.currentTitle!
if typing {
outputDisplay.text = outputDisplay.text! + String(number!)
} else {
outputDisplay.text = number!
}
typing = true
}
@IBAction func calculationPressed(sender: AnyObject) {
typing = false
firstNumber = Float(outputDisplay.text!)!
operation = sender.currentTitle!!
}
@IBAction func equalPressed(sender: AnyObject) {
secondNumber = Float(outputDisplay.text!)!
typing = false
//arithmetic operations
if operation == "+" {
result = firstNumber + secondNumber
}
else if operation == "-" {
result = firstNumber - secondNumber
}
else if operation == "✕" {
result = firstNumber * secondNumber
}
else if operation == "÷" {
result = firstNumber/secondNumber
}
// else if operation == "%" {
// result = (firstNumber * secondNumber)/100
// }
else if operation == "√" {
result = sqrt(firstNumber)
}
outputDisplay.text = "\(result)"
}
@IBAction func squareRoot(sender: AnyObject) {
result = sqrt(Float(outputDisplay.text!)!)
outputDisplay.text = "\(result)"
}
@IBAction func deletePressed(sender: AnyObject) {
if outputDisplay.text!.characters.count != 0 {
outputDisplay.text = outputDisplay.text!.substringToIndex(outputDisplay.text!.endIndex.predecessor())
}
else{
outputDisplay.text = "0"
}
}
@IBAction func plusMinus(sender: AnyObject) {
if(outputDisplay.text![outputDisplay.text!.startIndex] == "-"){
outputDisplay.text!.removeAtIndex(outputDisplay.text!.startIndex)
}else{
outputDisplay.text!.insert("-", atIndex: outputDisplay.text!.startIndex)
}
}
@IBAction func decimal(sender: AnyObject) {
let decimal = sender.currentTitle!
outputDisplay.text = outputDisplay.text! + decimal!
}
@IBAction func clear(sender: AnyObject) {
result = 0
firstNumber = 0
secondNumber = 0
outputDisplay.text = "0"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
與您的問題沒有關係,但是:爲什麼不使用switch case而不是if if else語句?我認爲switch case會更合適,因爲if語句必須測試每個表達式。 – user2203384