2016年12月25日 星期日

Swift3 簡單的鍵盤監控及AlertAction凍結

這邊就不多做解釋了,直接放上Code跟截圖跟大家分享!!


storyboard自行發揮摟~而我為了呈現會擋住textfield所以我故意放比較低

在這要比較注意的是,通常大多數會擋住textfield是因為沒有使用tableview
物件無法滑動的情況下就變成必須改變view的座標,但以下寫法並不適合過多欄位使用。



import UIKit

class ViewController: UIViewController ,UITextFieldDelegate{

    @IBOutlet weak var ShowInputLabel: UILabel!
    @IBOutlet weak var YouerTextField: UITextField!
    @IBOutlet weak var AlertTextLabel: UILabel!
    
    var singleFingerTap:UITapGestureRecognizer!
    var alert:UIAlertController!
   
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.YouerTextField.delegate = self

        //鍵盤監聽
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWasShow), name: .UIKeyboardWillShow, object: nil);
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWasHidden), name: .UIKeyboardWillHide, object: nil);
        singleFingerTap = UITapGestureRecognizer.init(target: self, action: #selector(handleSingleTap));
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
        NotificationCenter.default.removeObserver(self)
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func AlertBtn(_ sender: UIButton) {
         alert = UIAlertController(title: "Alert", message: "輸入下方欄位", preferredStyle: .alert);
        //加入textfieldalert
        alert.addTextField { (text:UITextField) in
            text.placeholder = "輸入"
           //監聽
            NotificationCenter.default.addObserver(self, selector: #selector(ViewController.alertCheck(notification:)), name: .UITextFieldTextDidChange, object: text);
        }
        let alertAction = UIAlertAction(title: "OK", style: .default) { (UIAlertAction) in
            self.AlertTextLabel.text = self.alert.textFields?.first?.text
            //釋放間聽
            NotificationCenter.default.removeObserver(self, name: .UITextFieldTextDidChange, object: nil);
        }
        
        alertAction.isEnabled = false//凍結OK
        alert.addAction(alertAction)
        self.present(alert, animated: true, completion: nil);
    }
    //凍結action(監控)
    func alertCheck(notification:Notification){
        alert = self.presentedViewController as! UIAlertController?
        if(alert != nil){
            let input = (alert.textFields?.first)! as UITextField
            let alertActionOK = alert.actions.last! as UIAlertAction
            //計算字元到多少未解凍
            alertActionOK.isEnabled = (input.text?.characters.count)! > 5
        }
    }
    
    @IBAction func OutPutBtn(_ sender: UIButton) {
        ShowInputLabel.text = YouerTextField.text
    }
//MARK: 鍵盤監聽動作(點view隱藏鍵盤)
    //鍵盤顯示
    func keyboardWasShow(notification:Notification){
        self.view.addGestureRecognizer(singleFingerTap)
        
        //獲取鍵盤高度
        let info = notification.userInfo!
        let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let hieght = keyboardFrame.height
        //改變view座標避免鍵盤擋到textfield(注意:欄位多時不適用)
       self.view.frame.origin.y = -hieght
    }
    //鍵盤收起
    func keyboardWasHidden(notification:Notification){
        self.view.removeGestureRecognizer(singleFingerTap)
        self.view.frame.origin.y = 0
        
    }
    func handleSingleTap(){
        YouerTextField.resignFirstResponder()
    }
    //MARK: 鍵盤按下return後的動作
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        if(textField.text != ""){
            self.OutPutBtn(.init(type:.custom))
        }
        //假設如果有多個欄位要動作時
        /*if(textField.isEqual("第一個欄位")){
            YouerTextField.becomeFirstResponder()//第二個欄位
        }else{
            self.OutPutBtn(.init(type:.custom))
        }*/
        
        return true
    }


}

出來的效果如圖:





2016年12月6日 星期二

swift 多國語系(國際化)

這做法步驟比較多一點,將以圖文解釋。。。
建議先做英文版,畢竟是國際通用語言
再來就是在專案完成後最後在做國際語言,雖然在某些地方會麻煩點。但是storyboard才不用一個一個自己建立ID,主要順續就看個人習慣摟~

選到專案Info下方有個Locallizations,點選下方+選擇你要加入的語言


選擇後會是這樣的(勾起表示要建立多國語系)


建立完成會顯示你所做的語系

再來建立Strings File


命名方式看你個人喜歡摟~


建立完成後會是這樣,這時左邊欄有個Localize...點下去


因為一開始做的語系是預設語系(英文)所以這邊不動他直接建立


建立後將右邊欄位所要增加的語系勾起後如右下圖所示會多出兩個檔案


勾起來後檔案多出外記得去檢查前面所新增的語系跟原本的語系有沒有多一個監控
原本為2 Files Localized要變成3 Files Localized
這樣才能確保code裡面所做的多國語系有用途喔!



下圖是storyboard得多國語系(預設語系為原先就製作好的storyboard)
以下為增加語系要做的動作



接下來要介紹的是如果code內使用多國語系



當然不是做好上方就好拉!!!除了APP名稱其餘都是透過code來詮釋如下:

 override func viewDidLoad() {
        super.viewDidLoad()
       
       //呼叫多國語系
        let labelLanguage = NSLocalizedString("Language", tableName: "InfoPlist", bundle: Bundle.main, value: "" , comment: "");
        //"Language"自訂一個key"InfoPlist"所建立的Strings Flie名稱
        
        //建立label
        let label:UILabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2, y: 200, width: 100, height: 30));
        label.text = labelLanguage//帶入多國語系的值
        self.view.addSubview(label);
    }
呈現出來結果如下:
小提醒:自己建立的key:vaule要注意一定要加入;要不然會有error喔

雖然很簡單,但我就遇過專案怎麼搞都沒有成功過T.T
最後還是建立全新的專案重做才正常。。。
寫的不是很好但希望有幫助參考這篇的你


swift 手勢動作回上一頁

今天要做的是手勢動作回到上一頁,會需要俄未作此動作的原因通常是因為沒有使用NavigayionController的情況下。不然一般如有使用原生就已經幫你做好了

剛好專案因某些設計問題沒有使用到NavigationController,又加上大多數用戶因方便已慣性使用手勢動作(左邊滑向右邊)就可返回上一頁,故花了點時間找資料。
參考連結如下:https://www.hackingwithswift.com/example-code/uikit/how-to-detect-edge-swipes

要怎麼做呢???這是純code的做法
所使用到的是UIPanGestureRecognizer中的方法

首先在viewdidload內執行
let edgePan = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(screenEdgeSwiped))
        edgePan.edges = .left
        

        view.addGestureRecognizer(edgePan)

然後再建立一個func來執行動作
 func screenEdgeSwiped(_ recognizer: UIScreenEdgePanGestureRecognizer) {
        if recognizer.state == .recognized {
            print("Screen edge swiped!")
            let V1 = self.storyboard?.instantiateViewController(withIdentifier: "V1")
            self.present(V1!, animated: true, completion: nil)
        }
    }
這樣就完成了回到上一頁的手勢動作摟~