iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 7
0
Software Development

iOS 從 Objective-c 進化為 Swift 的 30天之旅系列 第 7

[Day 7] Swift 簡易計算機 (一)

Safe Area

原本在網路上查詢以下程式碼
抓取Safe Area
但是很常時候在載入時沒辦法即時抓取
會延遲個0.幾秒才會顯示

    view.safeAreaInsets
    view.safeAreaLayoutGuide

改使用以下程式碼後
就可以在載入時迅速抓取安全高度
iPhoneX 記得要扣掉top與bottom 兩個喔喔喔喔

    let insets = UIApplication.shared.delegate?.window??.safeAreaInsets ?? UIEdgeInsets.zero
    insets.top
    insets.bottom

計算機設計

取Button Tag

    @IBAction func clickNumberBtn(sender:UIButton) {
        sender.tag
    }

這邊還是要抱怨一下
從Void 換成 func
不同Class呼叫並不用import
並且竟然不用

view.delegate = self

以下就是我AddSubview
不知道對不對
delegate不用等於self嗎
What!?

addView = AddUIView.init(frame: CGRect.init(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height));
        addView.backgroundColor = UIColor.blue
        self.view.addSubview(addView);


按鈕部分使用tag做區分
指向同一個target
不管"數字"與"數學"按鈕都是這樣做區分喔喔

addView.oneBtn.addTarget(self,
                                  action: #selector(clickNumberBtn),
                                  for: .touchUpInside)

計算機自己初想是這樣
每次輸入一次
將原本數字X10 + 輸入數字

按下數學加減乘除後
紀錄加減乘除 tag
然後 priceA丟給priceB
priceA 在反覆第一步

數字按鈕 target

@IBAction func clickNumberBtn(sender:UIButton) {
    if isMath {
        priceB = priceA
        priceA = 0
        isMath = false
    }
    if priceA == 0 {
        priceA = sender.tag
    }else{
        priceA = priceA * 10 + sender.tag
    }
    addView.uploadUI(price: priceA)
}

數學按鈕 target

@IBAction func clickConfirmBtn(sender:UIButton) {
    switch mathStatus {
    case 10:
        priceA = priceA + priceB
        print("+")
    case 11:
        priceA = priceA - priceB
        print("-")
    case 12:
        priceA = priceA * priceB
        print("*")
    case 13:
        priceA = priceA / priceB
        print("/")
    default:
        print("faill")
    }

    addView.uploadUI(price: priceA)
    isMath = false
    mathStatus = 0
}

使用switch case
基本上好像跟OC一樣?開心?

目前進度

加減乘除 target基本上都接好了
但除法有點問題
除不出來
應該是我偷懶使用int去存數字
除法應該會到float!? 明天再改改看
小數點按鈕 也還沒做
預計明天一起補上

最新文章

金魚也學得會的「KD指標」! 什麼是KD黃金交叉、KD死亡交叉 - 2021年

RSI指標? 看我就好 新手好入門


上一篇
[Day 6] Swift MVC 簡易分層練習
下一篇
[Day 8] Swift 使用單例 singleton 做一個 全域變數
系列文
iOS 從 Objective-c 進化為 Swift 的 30天之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言