1. 程式人生 > >【Swift】鍵盤高度

【Swift】鍵盤高度

 //鍵盤監聽
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notif:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardwillhidden(noti:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil
)
    //鍵盤監聽事件
    func keyboardWillChange(notif:NSNotification){
        let userInfo:NSDictionary = notif.userInfo! as NSDictionary;
        let keyBoardInfo2: AnyObject? = userInfo.object(forKey: UIKeyboardFrameEndUserInfoKey) as AnyObject;
        let endY = keyBoardInfo2?.cgRectValue.size.height
        let
aTime = userInfo.object(forKey: UIKeyboardAnimationDurationUserInfoKey) as! TimeInterval UIView.animate(withDuration: aTime) { [weak self]() -> Void in self?.view.transform = CGAffineTransform(translationX: 0, y: -endY! / 3 * 2) } }
    //鍵盤監聽事件
    func keyboardwillhidden(noti:NSNotification
){ let userInfo:NSDictionary = noti.userInfo! as NSDictionary; let aTime = userInfo.object(forKey: UIKeyboardAnimationDurationUserInfoKey) as! TimeInterval UIView.animate(withDuration: aTime) { [weak self]() -> Void in self?.view.transform = CGAffineTransform.identity } }