iOS 键盘输入处理(UIScrollView,一般不需要滚动的页面)

  //这里,有时候会在keyboardWillChangeFrame后面执行,通知方法里面要做非空判断。

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

         isEditTextField = textField

        return true

    }

一般不需要滚动的页面

//键盘将要改的通知方法

    @objc func keyboardWillChangeFrame(notifi: Notification) {

        if let endFrame = notifi.userInfo!["UIKeyboardFrameEndUserInfoKey"] as? NSValue {

            guard isEditTextField != nil else{

                return

            }

            let rect = endFrame.cgRectValue

            let value = (self.view.frame.size.height-(isEditTextField?.frame.origin.y)!-(isEditTextField?.frame.size.height)!-30)-rect.size.height//

            

            UIView.animate(withDuration: TimeInterval(0.25), animations: {

                //键盘覆盖了输入框

                if value < 0 {

                    //已经上移动一次了,就不用移动了

                    if self.view.frame.origin.y == 0{

                        self.view.frame.origin.y += value

                    }

                }

                if rect.origin.y == UIScreen.main.bounds.height { //键盘将要收起

                    self.view.frame.origin.y = 0

                }

            })

        }

    }

UISCrollView页面的键盘出入

//键盘将要改的通知方法

    @objc func onKeyboardWillShowFrame(notification: NSNotification) {

        let dict = NSDictionary(dictionary: notification.userInfo!)

        let keyboardFrame = (dict[UIKeyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue

        let duration = dict[UIKeyboardAnimationDurationUserInfoKey] as! Double

        //点击下一步的跳转

        if keyboardShown == true  {

            UIView.animate(withDuration: duration, animations: { () -> Void in

                 //键盘挡住内容,需要加大contentView的高度

                self.scrollView.frame.size.height = self.view.frame.size.height-(keyboardFrame?.size.height)!

                 print(self.scrollView.contentSize.height)

                //已经改变一次就不再改变

                if self.needChangeContent == true {}else{

                    self.scrollView.contentSize.height

                        += (keyboardFrame?.size.height)!

                    print(self.scrollView.contentSize.height)

                    self.scrollView.scrollRectToVisible((self.isEditTextField?.frame)!, animated: true)

                    self.needChangeContent = true

                }

                //键盘没有挡住内容

            })

         //点击一个输入框的跳转

        }else{

            UIView.animate(withDuration: duration, animations: { () -> Void in

                self.scrollView.frame.size.height = self.view.frame.size.height-(keyboardFrame?.size.height)!

                //+30 是为了使textFiled不要紧贴输入法

                 self.scrollView.scrollRectToVisible((self.isEditTextField?.frame)!, animated: true)

            })

            self.needChangeContent = false

        }

         keyboardShown = true

    }

    @objc func onKeyboardWillHideFrame(notification: NSNotification) {

        let dict = NSDictionary(dictionary: notification.userInfo!)

        let duration = dict[UIKeyboardAnimationDurationUserInfoKey] as! Double

        let keyboardFrame = (dict[UIKeyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue

        UIView.animate(withDuration: duration, animations: { () -> Void in

            //让scrollView还原

            self.scrollView.frame.size.height = self.view.frame.size.height

            //修改了contentView的高度,需要还原

            if self.needChangeContent == true{

                self.scrollView.contentSize.height -= (keyboardFrame?.size.height)!

            }

        })

        keyboardShown = false

    }

看起来比较复杂,还需要再精简一些的。

猜你喜欢

转载自blog.csdn.net/weixin_39872861/article/details/82801853