Rf-11 換算 App 複利計算

確診後睽違好幾天的作品先附上熱騰騰的gif擋

主題發想

這週一個衰衰的確診沒辦法到現場上課,癱在床上四天好不容易恢復趕快來補作業了^^

這主題完全也沒想,因為這就是我蠻想要的一個App方便我計算一下複利的力量!!

程式使用

元件部分使用了UITextField、UISwitch、UISlider、UILabel、UIScrollView、UIButton、UIPageControl、UISegmentedControl

這邊有兩個計算頁面,但UILabel和Button而我只各拉一個,這邊可用程式去判斷正在哪個頁面就計算哪個頁面~~

 override func viewDidLoad() {
super.viewDidLoad()

//在畫面上加入一個動畫的箭頭圖片
let imageView = UIImageView(frame: CGRect(x: 17, y: 450, width: 200, height: 130))
view.addSubview(imageView)
let animatedImage = UIImage.animatedImageNamed("animated-arrow-image-0553-", duration: 1)
imageView.image = animatedImage


// 所有計算部分的輸入框美化
cashDividendInput.layer.cornerRadius = 5
cashDividendInput.layer.borderWidth = 3
cashDividendInput.layer.borderColor = CGColor(red: 241/255, green: 91/255, blue: 108/255, alpha: 0.7)
cashDividendInput.backgroundColor = UIColor(red: 0/255, green: 174/255, blue: 157/255, alpha: 0.7)
stockDividendInput.layer.cornerRadius = 5
stockDividendInput.layer.borderWidth = 3
stockDividendInput.layer.borderColor = CGColor(red: 241/255, green: 91/255, blue: 108/255, alpha: 0.7)
stockDividendInput.backgroundColor = UIColor(red: 0/255, green: 174/255, blue: 157/255, alpha: 0.7)
stockPrice.layer.cornerRadius = 5
stockPrice.layer.borderWidth = 3
stockPrice.layer.borderColor = CGColor(red: 241/255, green: 91/255, blue: 108/255, alpha: 0.7)
stockPrice.backgroundColor = UIColor(red: 0/255, green: 174/255, blue: 157/255, alpha: 0.7)
nowQuantity.layer.cornerRadius = 5
nowQuantity.layer.borderWidth = 3
nowQuantity.layer.borderColor = CGColor(red: 241/255, green: 91/255, blue: 108/255, alpha: 0.7)
nowQuantity.backgroundColor = UIColor(red: 0/255, green: 174/255, blue: 157/255, alpha: 0.7)
buyAyear.layer.cornerRadius = 5
buyAyear.layer.borderWidth = 3
buyAyear.layer.borderColor = CGColor(red: 241/255, green: 91/255, blue: 108/255, alpha: 0.7)
buyAyear.backgroundColor = UIColor(red: 0/255, green: 174/255, blue: 157/255, alpha: 0.7)
howYear.layer.cornerRadius = 5
howYear.layer.borderWidth = 3
howYear.layer.borderColor = CGColor(red: 241/255, green: 91/255, blue: 108/255, alpha: 0.7)
howYear.backgroundColor = UIColor(red: 0/255, green: 174/255, blue: 157/255, alpha: 0.7)

yieldStockPrice.layer.cornerRadius = 5
yieldStockPrice.layer.borderWidth = 3
yieldStockPrice.layer.borderColor = CGColor(red: 241/255, green: 91/255, blue: 108/255, alpha: 0.7)
yieldStockPrice.backgroundColor = UIColor(red: 0/255, green: 174/255, blue: 157/255, alpha: 0.7)
yieldNowQuantity.layer.cornerRadius = 5
yieldNowQuantity.layer.borderWidth = 3
yieldNowQuantity.layer.borderColor = CGColor(red: 241/255, green: 91/255, blue: 108/255, alpha: 0.7)
yieldNowQuantity.backgroundColor = UIColor(red: 0/255, green: 174/255, blue: 157/255, alpha: 0.7)
yieldPercent.layer.cornerRadius = 5
yieldPercent.layer.borderWidth = 3
yieldPercent.layer.borderColor = CGColor(red: 241/255, green: 91/255, blue: 108/255, alpha: 0.7)
yieldPercent.backgroundColor = UIColor(red: 0/255, green: 174/255, blue: 157/255, alpha: 0.7)
yieldBuyAyear.layer.cornerRadius = 5
yieldBuyAyear.layer.borderWidth = 3
yieldBuyAyear.layer.borderColor = CGColor(red: 241/255, green: 91/255, blue: 108/255, alpha: 0.7)
yieldBuyAyear.backgroundColor = UIColor(red: 0/255, green: 174/255, blue: 157/255, alpha: 0.7)
yieldHowYear.layer.cornerRadius = 5
yieldHowYear.layer.borderWidth = 3
yieldHowYear.layer.borderColor = CGColor(red: 241/255, green: 91/255, blue: 108/255, alpha: 0.7)
yieldHowYear.backgroundColor = UIColor(red: 0/255, green: 174/255, blue: 157/255, alpha: 0.7)
func cmdStyles(to view: UIView) {
view.layer.cornerRadius = 5
view.layer.borderWidth = 3
view.layer.borderColor = CGColor(red: 241/255, green: 91/255, blue: 108/255, alpha: 0.7)
view.backgroundColor = UIColor(red: 0/255, green: 174/255, blue: 157/255, alpha: 0.7)
}

cmdStyles(to: cashDividendInput)
cmdStyles(to: stockDividendInput)
cmdStyles(to: stockPrice)
cmdStyles(to: nowQuantity)
cmdStyles(to: buyAyear)
cmdStyles(to: howYear)
cmdStyles(to: yieldStockPrice)
cmdStyles(to: yieldNowQuantity)
cmdStyles(to: yieldPercent)
cmdStyles(to: yieldBuyAyear)
cmdStyles(to: yieldHowYear)

這邊viewDidLoad()的地方我放上一個gif的圖以及美化一下輸入框~~
上面是原始寫法下面是更簡潔~~

寫完才想到可以用func來簡化 恨啊!!!!

    //股票複利計算的方法
func stockCompound() {
if let cash = Double(cashDividendInput.text!),
let stock = Double(stockDividendInput.text!),
let price = Double(stockPrice.text!),
let now = Double(nowQuantity.text!),
let buy = Double(buyAyear.text!),
let how = Int(howYear.text!) {

let cashAll = cash * now
let stockAll = stock * 100 * (now / 1000)
var allTotelyear = (cashAll / price) + now + stockAll


if switchAgainOn.isOn {
for _ in 1...how {
let cashAlltwo = cash * allTotelyear
let stockAlltwo = stock * 100 * (allTotelyear / 1000)
allTotelyear = (cashAlltwo / price) + allTotelyear + stockAlltwo + (buy * 1000)
logger.log("\(cashAlltwo) \(stockAlltwo) \(allTotelyear)")
}
outPutAll.text = "複利\(how)年後你股票張數變成\(round(allTotelyear))股\n大約為\(round(allTotelyear/1000))張"
}else{
allTotelyear -= cashAll / price
for _ in 1...how {
let stockAlltwo = stock * 100 * (allTotelyear / 1000)
allTotelyear = allTotelyear + stockAlltwo + (buy * 1000)
}
outPutAll.text = "複利\(how)年後你股票張數變成\(round(allTotelyear))股\n大約為\(round(allTotelyear/1000))張"
}

} else {
outPutAll.text = "請輸入有效的數字"
}
}

//殖利率計算的方法
func stockYield() {
if let yieldPrice = Double(yieldStockPrice.text!),
let yieldNow = Double(yieldNowQuantity.text!),
let percent = Double(yieldPercent.text!),
let yieldBuy = Double(yieldBuyAyear.text!),
let yieldHow = Int(yieldHowYear.text!){

let yieldAll = yieldPrice * yieldNow * (percent / 100)
var yieldAlltotel = yieldPrice * yieldNow + yieldAll

for _ in 1...yieldHow {
yieldAlltotel = yieldAlltotel * (percent / 100) + yieldAlltotel + yieldBuy
}
outPutAll.text = "複利\(yieldHow)年後\n你股票價值變成\n\(round(yieldAlltotel))元\n"
}else{
outPutAll.text = "請輸入有效的數字"

}
}

這邊我把股票複利和殖利率,這兩個計算分別寫成func,當button按下去才調用~~

因為這次都是UITextField,所以使用了 if let ,將輸入框中的文字轉換成數字,如果轉換成功,則將轉換後的值給相應的變數 cashstockpricenowbuyhow~~

如果轉換失敗如使用者輸入的是無效的數字,則 if let 條件將會失敗,並顯示 "請輸入有效的數字"~~

如果成功經過計算後使用outPutAll.text存入UILabel顯示資訊~~

@IBAction func allCalculate(_ sender: Any) {
if stockSelect.selectedSegmentIndex == 0 {
stockCompound()
}else if stockSelect.selectedSegmentIndex == 1{
stockYield()
}
view.endEditing(true)
}

這段我直接拿stockSelect.selectedSegmentIndex來判斷分頁目前的位置,

如果是0代表正在股票複利計算頁面

反之如果是1代表正在殖利率計算頁面

順便加上view.endEditing(true)再計算按鈕上這樣只要按下按鈕就收鍵盤

//分頁控制元件的事件處理方法,切換到指定分頁
@IBAction func stockChangePage(_ sender: Any) {
let point = CGPoint(x: scollView.bounds.width * CGFloat((sender as AnyObject).currentPage), y: 0)
scollView.setContentOffset(point, animated: true)
pageNum = stockPageControl.currentPage
pageSync()
}

//分段控制元件的事件處理方法,切換到指定分頁
@IBAction func stockSelectPage(_ sender: Any) {
let point = CGPoint(x: scollView.bounds.width * CGFloat(stockSelect.selectedSegmentIndex), y: 0)
scollView.setContentOffset(point, animated: true)
pageNum = stockSelect.selectedSegmentIndex
pageSync()
}
}



extension ViewController: UIScrollViewDelegate {
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let page = scrollView.contentOffset.x / scrollView.bounds.width
stockPageControl.currentPage = Int(page)
stockSelect.selectedSegmentIndex = Int(page)
}
}

這邊的判斷分頁切換分頁的方法可以參考peter的方法~~

可以讓UISegmentedControl和UIPageControl同步變化並切換~~

心得分享

這作業我覺得最難的地方在於,判斷分頁與切換分頁這邊的程式碼,來回看了幾遍才了解UISegmentedControl該怎麼修改,還有optional的使用另外還有peter上課講的log使用,沒這個東西我中間計算部分可能要debug很久可以印出我要的資訊超好用,這作業先這樣快點來補作業進度了^^

--

--