通过小项目开发的实例,学习swift。
为大家分享swift实现简单的计算器项目,具体如下:
// // ViewController.swift // 计算器 // // Created by yaoyezhifan on 2022/8/23. // import UIKit class ViewController: UIViewController { var numOne = UITextField() var numTwo = UITextField() var sum = UILabel() var cleanButton = UIButton() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. //创建第一个输入的加数,即创建文本框 let numOne = UITextField(frame: CGRect(x: 35, y: 120, width: 75, height: 35)) numOne.placeholder = "输一个数" numOne.layer.borderColor = UIColor.lightGray.cgColor//边框色 numOne.layer.borderWidth = 2//边框的宽度 numOne.layer.cornerRadius = 5//边框的弧度 self.view.addSubview(numOne)//向view画面中,添加文本框 numOne.keyboardType = UIKeyboardType.numberPad//设定输入键盘为数字型 self.numOne = numOne//通过self保存创建的文本框 //创建“+”号标签 let add = UILabel(frame: CGRect(x: 130, y: 120, width: 40, height: 30)) add.text = "+" add.font.withSize(20)//字体大小 self.view.addSubview(add)//向view画面中,添加标签 // 第二个数 let numTwo = UITextField(frame: CGRect(x: 165, y: 120, width: 75, height: 35)) numTwo.placeholder = "输一个数" numTwo.layer.borderColor = UIColor.lightGray.cgColor//边框色 numTwo.layer.borderWidth = 2//边框的宽度 numTwo.layer.cornerRadius = 5//边框的弧度 self.view.addSubview(numTwo)//向view画面中,添加文本框 numTwo.keyboardType = UIKeyboardType.numberPad//设定输入键盘为数字型 self.numTwo = numTwo//通过self保存创建的文本框 //创建“=”号标签 let equalTo = UILabel(frame: CGRect(x: 275, y: 120, width: 40, height: 30)) equalTo.text = "=" equalTo.font.withSize(20)//字体大小 self.view.addSubview(equalTo)//向view画面中,添加标签 // sum 和 let sum = UILabel(frame: CGRect(x: 310, y: 120, width: 40, height: 30)) sum.textAlignment = .center//对齐方式:居中对齐 sum.font.withSize(20)//字体大小 sum.text = "0" self.view.addSubview(sum)//向view画面中,添加标签 sum.layer.borderWidth = 2//边框的宽度 sum.layer.borderColor = UIColor.lightGray.cgColor//边框色 self.sum = sum // “计算”按钮,点击后实现计算功能 let button: UIButton = UIButton(frame: CGRect(x: 145, y: 200, width: 100, height: 40)) self.view.addSubview(button) button.backgroundColor = UIColor.lightGray//边框色 button.setTitle("计算", for: .normal)//按钮title button.setTitleColor(.black, for: .normal)//按钮title色 button.addTarget(self, action: #selector(Onclick), for: .touchUpInside)//点击按钮触发事件 } //点击“计算”按钮方法实现 @objc func Onclick () { let sum = NSString(string: self.numOne.text ?? "").intValue + NSString(string: self.numTwo.text ?? "").intValue self.sum.text = String(String(sum))//计算总和并显示 self.addCleanButton()//添加清除按钮 } //清除按钮 func addCleanButton(){ let cleanButton:UIButton = UIButton(frame: CGRect(x: 145, y: 250, width: 100, height: 40)) cleanButton.setTitle("清除", for: .normal)//按钮title cleanButton.setTitleColor(.red, for: .normal)//按钮title色 self.view.addSubview(cleanButton) cleanButton.addTarget(self, action: #selector(cleanButtonClick), for: .touchUpInside)//点击按钮触发事件 self.cleanButton = cleanButton } //清除按钮方法实现 @objc func cleanButtonClick(){ self.sum.text = "0" self.numOne.text = "" self.numTwo.text = "" if(self.sum.text=="0"){ self.cleanButton.isHidden = true } } }
最终实现结果:
自我感觉可以看的懂,希望对初学者有帮助~~