prettyprint

2017年5月9日 星期二

範例:豆漿食譜

Objective

製作豆漿時,豆子與水的比例相當重要,此範例提供方便計算,而無需人為的換算

UI 

事實上,此 UI 僅提供簡單設計,重點在強調功能面。有關各 UI Component 日後再調整。



Code

//
//  ViewController.swift
//  FoodRecipeUnit
//
//  Created by Elvis Meng on 2017/5/8.
//  Copyright © 2017 Elvis Meng. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    
    var soyBeanUnit =  ["公克",""]
    var waterUnit = ["",""]
    var sugarUnit = ["公克",""]
    var brixUnit = ["微甜",""]
    
    var soyBeanUnitSelected = "公克"
    var waterUnitSelected = ""
    var sugarUnitSelected = "公克"
    var brixUnitSelected = "微甜"
    
    var soyBeanAmount = 0.0
    var waterAmount = 0.0
    var sugarAmount = 0.0
    
    @IBOutlet weak var soyBeanWeight: UITextField!
    @IBOutlet weak var waterWeight: UITextField!
    @IBOutlet weak var sugarWeight: UITextField!
    
    @IBAction func soyBeanEditOnExit(_ sender: UITextField) {
        
        if soyBeanUnitSelected == "" {
            soyBeanAmount = 600.0 * Double(soyBeanWeight.text!)!
        } else {
            soyBeanAmount = Double(soyBeanWeight.text!)!
        }
        
        if waterUnitSelected == "" {
            waterWeight.text = String(soyBeanAmount * 7.0 / 1500.0)
        } else {
            waterWeight.text = String(soyBeanAmount * 7.0 * 2500 / 1500.0)
        }
        
    }
    
    @IBAction func waterEditOnExit(_ sender: UITextField) {
        
        if waterUnitSelected == ""  {
            waterAmount = 2500.0 * Double(waterWeight.text!)!
        } else {
            waterAmount = Double(waterWeight.text!)!
        }
        
        if soyBeanUnitSelected == "" {
            soyBeanWeight.text = String(waterAmount * 2.5 / (7.0 * 2500.0))
            
        } else {
            soyBeanWeight.text = String(waterAmount * 2.5 / (7.0 * 2500.0) * 600.0)
        }
    }

    
    @IBAction func calcBtn(_ sender: UIButton) {
        soyBeanWeight.text = nil
        waterWeight.text = nil
        sugarWeight.text = nil
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch pickerView.tag {
        case 0:
            return soyBeanUnit.count
        case 1:
            return waterUnit.count
        case 2:
            return sugarUnit.count
        case 3:
            return brixUnit.count
        default:
            return 0
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        
        switch pickerView.tag {
        case 0:
            soyBeanUnitSelected = soyBeanUnit[row]
            return soyBeanUnit[row]
        case 1:
            waterUnitSelected = waterUnit[row]
            return waterUnit[row]
        case 2:
            sugarUnitSelected = sugarUnit[row]
            return sugarUnit[row]
        case 3:
            brixUnitSelected = brixUnit[row]
            return brixUnit[row]
        default:
            return "error"
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}





Test



Summary

1. 此範例不實作糖與甜度,此部分可留待日後實作
2. 在 textField 中,輸入資料,按下 enter,會啟動 EditingOnExit 事件,可自動帶出換算結果,而 UI 可省去 Calculate 這個 Button


Reference

1. UITextField, https://developer.apple.com/reference/uikit/uitextfield
2. UIViewController, https://developer.apple.com/reference/uikit/uiviewcontroller




/end

2017年5月5日 星期五

範例:簡單型計算機

Objective


此實作參考 [1],而試著將 Objective-C 程式 Port 到 Swift 3.0 程式

Lab


1. 新增 Single View Application 專案
2. UI 設計


Code

1. File Calculation.swift

//
//  Calculation.swift
//  myCaculatorDemo
//
//  Created by Elvis Meng on 2017/5/4.
//  Copyright © 2017 Elvis Meng. All rights reserved.
//

import Foundation

class Calculation : NSObject {

    var operandA : Float?
    var operandB : Float?
    var op : Character?
    var isFirstOperand : Bool
    var result : Float?
    
    override init() {
        operandA = 0.0
        operandB = 0.0
        op = nil
        isFirstOperand = true
        result = 0.0
    }
    
    func calculateResult(operandA : Float, operandB : Float, op : Character) -> Float {
        switch op {
        case "+" :
            result = operandA + operandB
        case "-" :
            result = operandA - operandB
        case "*" :
            result = operandA * operandB
        case "/" :
            result = operandA / operandB
        default :
            print("error")
        }
        return result!
    }
    

}

2. File : ViewController.swift

//
//  ViewController.swift
//  myCaculatorDemo
//
//  Created by Elvis Meng on 2017/4/25.
//  Copyright © 2017 Elvis Meng. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var displayResult: UILabel!
    
    var cal:Calculation = Calculation()
    var isFirstDigit = true
    var hasTapEqual = false
    var operand:Float = 0.0
    var result:Float = 0.0
    var digitCount = 0
    var digit = 0
    
    func processCalc(op:Character){
        if (cal.isFirstOperand){
            cal.operandA = operand
            cal.isFirstOperand = false
        }
        else if (hasTapEqual){
            cal.operandA = result
            cal.operandB = operand
            hasTapEqual = false
        }
        else {
            cal.operandB = operand
            result = cal.calculateResult(operandA: cal.operandA!, operandB: cal.operandB!, op: op)
            displayProcess(num: result)
            cal.operandA = result
        }
        cal.op = op
        operand = 0.0
        digitCount = 0
    }
    
    @IBAction func tapDigit(_ sender: UIButton) {
        digit = sender.tag
        if hasTapEqual == true {
            displayResult.text = "0.0"
            cal.operandA = 0
            cal.operandB = 0
            cal.isFirstOperand = true
            result = 0
            operand = 0
            isFirstDigit = true
            hasTapEqual = false
        }
        if (isFirstDigit && digit == 0){
            isFirstDigit = true
        } else {
            if (digitCount >= 15) {
                return
            }
            isFirstDigit = false
            operand = operand * 10 + Float(digit)
            displayProcess(num: operand)
        }
        digitCount += 1
    }
    
    @IBAction func tapPlus(_ sender: UIButton) {
        processCalc(op: "+")
    }
    
    @IBAction func tapMinus(_ sender: UIButton) {
        processCalc(op: "-")
    }

    @IBAction func tapMultiply(_ sender: UIButton) {
        processCalc(op: "*")
    }
    
    @IBAction func tapDivide(_ sender: UIButton) {
        processCalc(op: "/")
    }
    
    
    @IBAction func tapEqual(_ sender: UIButton) {
        if (cal.isFirstOperand == false){
            cal.operandB = operand
            result = cal.calculateResult(operandA: cal.operandA!, operandB: cal.operandB!, op: cal.op!)
            self.displayProcess(num: result)
            cal.operandA = result
            hasTapEqual = true
        }
    }
    

    @IBAction func tapAC(_ sender: UIButton) {
        displayResult.text = "0.0"
        cal.operandA = 0
        cal.operandB = 0
        cal.isFirstOperand = true
        result = 0
        operand = 0
        isFirstDigit = true
        hasTapEqual = false
    }
    
    func displayProcess(num:Float){
        displayResult.text = String(num)

    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let cal:Calculation = Calculation()
        cal.isFirstOperand = true
        isFirstDigit = true
        hasTapEqual = false
        displayResult.text = String(0.0)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}



Test






Reference


1. Chapter 24, 範例程式五:簡易計算器實作, P24-27, “學會Objective-C 的24堂課“,  蔡明志






/end

範例:撥打電話

Objective


學習使用 Button 元件,同時處理多按鈕共用事件

Lab


1. 建立 Single View Application
2. UI 設計

在此畫面需加入 2 張 image,一個作為 Phone Call,另一個為 Phone Hangup


Code

//
//  ViewController.swift
//  MyPhoneDemo
//
//  Created by Elvis Meng on 2017/5/5.
//  Copyright © 2017 Elvis Meng. All rights reserved.
//

import UIKit

var str:String = ""

class ViewController: UIViewController {

    @IBOutlet weak var displayPhoneNumber: UILabel!
    
    @IBAction func tapDigitPad(_ sender: UIButton) {
        str = str + (sender.titleLabel?.text)!
        displayPhoneNumber.text = str
    }
    
    @IBAction func phoneCall(_ sender: UIButton) {
        let url = URL(string: "tel:"+str)
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url!, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url!)
        }
    }
    
    @IBAction func phoneHangUp(_ sender: UIButton) {
        str = ""
        displayPhoneNumber.text = ""
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


Test




Summary


此範例並處理 Phone Call,也處理 Phone Up


 /end
prettyPrint();