Swift實(shí)現(xiàn)簡(jiǎn)單計(jì)算器項(xiàng)目
更新時(shí)間:2022年01月26日 09:35:07 作者:mjbaishiyun
這篇文章主要為大家詳細(xì)介紹了Swift實(shí)現(xiàn)簡(jiǎn)單計(jì)算器項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Swift實(shí)現(xiàn)簡(jiǎn)單計(jì)算器項(xiàng)目的具體代碼,供大家參考,具體內(nèi)容如下
// // ?ViewController.swift // ?計(jì)算器 // // ?Created by 悅兌科技 on 15/1/14. // ?Copyright (c) 2015年 BSY. All rights reserved. // import UIKit class ViewController: UIViewController { ? ? var numOne = UITextField() ? ? var numTwo = UITextField() ? ? var cleanButton = UIButton() ? ? var sum = UILabel() ? ? override func viewDidLoad() { ? ? ? ? super.viewDidLoad() ? ? ?? ? ? ? ? self.view.backgroundColor = UIColor.brownColor()? ? ? ? ? [self .addAllSubViews()] ? ? } ? ? func addAllSubViews() ? ? { ? ? ? ? // 計(jì)算 ? ? ? ? var button:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton ? ? ? ? var frame = CGRectMake(100, 300, 100, 30) ? ? ? ? button.frame = frame ? ? ? ? self.view.addSubview(button) ? ? ? ? button.backgroundColor = UIColor.lightGrayColor() ? ? ? ? button.setTitle("計(jì)算", forState: UIControlState.Normal) ? ? ? ? button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal) ? ? ? ? button.addTarget(self, action: "OnClick", forControlEvents: UIControlEvents.TouchUpInside) ? ? ? ? // 第一個(gè)數(shù) ? ? ? ? var numOne = UITextField(frame: CGRectMake(10, 200, 80, 30)) ? ? ? ? numOne.placeholder = "輸一個(gè)數(shù)" ? ? ? ? numOne.layer.borderColor = UIColor.lightGrayColor().CGColor ? ? ? ? numOne.layer.borderWidth = 2 ? ? ? ? numOne.layer.cornerRadius = 5 ? ? ? ? self.view.addSubview(numOne) ? ? ? ? numOne.keyboardType = UIKeyboardType.NumberPad ? ? ? ? self.numOne = numOne ? ? ? ? ? var add = UILabel(frame: CGRectMake(100, 200, 20, 30)) ? ? ? ? add.text = "+" ? ? ? ? add.font.fontWithSize(20) ? ? ? ? self.view.addSubview(add) ?? ? ? ? ? // 第二個(gè)數(shù) ? ? ? ? var numTwo = UITextField(frame: CGRectMake(120, 200, 80, 30)) ? ? ? ? numTwo.placeholder = "輸一個(gè)數(shù)" ? ? ? ? numTwo.layer.borderColor = UIColor.lightGrayColor().CGColor ? ? ? ? numTwo.layer.borderWidth = 2 ? ? ? ? numTwo.layer.cornerRadius = 5 ? ? ? ? self.view.addSubview(numTwo) ? ? ? ? numTwo.keyboardType = UIKeyboardType.NumberPad ? ? ? ? self.numTwo = numTwo ? ? ? ? var equalTo = UILabel(frame: CGRectMake(210, 200, 20, 30)) ? ? ? ? equalTo.text = "=" ? ? ? ? equalTo.font.fontWithSize(20) ? ? ? ? self.view.addSubview(equalTo) ? ? ? ?? ? ? ? ? // sum ?和 ? ? ? ? var sum = UILabel(frame: CGRectMake(230, 200, 80, 30)) ? ? ? ? sum.textAlignment = NSTextAlignment.Center ? ? ? ? sum.font.fontWithSize(20) ? ? ? ? sum.text = "0" ? ? ? ? self.view.addSubview(sum) ? ? ? ? sum.layer.borderWidth = 2 ? ? ? ? sum.layer.borderColor = UIColor.lightGrayColor().CGColor ? ? ? ? self.sum ?= sum ? ? } ? ? /** ? ? 計(jì)算按鈕 ? ? */ func OnClick() { ? ? var sum = ?NSString(string: self.numOne.text).intValue + NSString(string: self.numTwo.text).intValue ? ? self.sum.text = NSString(string: String(sum)) ? ? [self .addCleanButton()] } ? ? /** ? ? 點(diǎn)擊界面事件 ? ? */ ? ? override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { ? ? ? ? self.numOne.resignFirstResponder() ? ? ? ? self.numTwo.resignFirstResponder() ? ? } ? ? /** ? ? 清除按鈕 ? ? */ ? ? func addCleanButton(){ ? ? ? ? var cleanButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton ? ? ? ? var frame = CGRectMake(220, 240, 50, 30) ? ? ? ? cleanButton.frame = frame ? ? ? ? cleanButton.setTitle("清除", forState: UIControlState.Normal) ? ? ? ? cleanButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal) ? ? ? ? self.view.addSubview(cleanButton) ? ? ? ? ? ? ? ? cleanButton.addTarget(self, action: "cleanButtonClick", forControlEvents: UIControlEvents.TouchUpInside) ? ? ? ? self.cleanButton = cleanButton ? ? } ? ? /** ? ? 清除按鈕方法實(shí)現(xiàn) ? ? */ ? ? func cleanButtonClick(){ ? ? self.sum.text = "0" ? ? self.numOne.text = "" ? ? self.numTwo.text = "" ? ? ? ? if(self.sum.text=="0"){ ? ? ? ? self.cleanButton.hidden = true ? ?? ? ? ? ? } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
swift實(shí)現(xiàn)自定義圓環(huán)進(jìn)度提示效果
這篇文章主要為大家詳細(xì)介紹了swift實(shí)現(xiàn)自定義圓環(huán)進(jìn)度提示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05Swift中用到extension的一些基本的擴(kuò)展功能講解
這篇文章主要介紹了Swift的一些基本的擴(kuò)展功能,即extension關(guān)鍵字的使用,需要的朋友可以參考下2015-11-11LeetCode?刷題?Swift?兩個(gè)數(shù)組的交集
這篇文章主要為大家介紹了LeetCode?刷題?Swift?兩個(gè)數(shù)組的交集示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09