键盘动画延迟

时间:2017-10-07 09:49:19

标签: ios swift

我正在尝试在键盘显示和隐藏时移动滚动视图内容。我正在做基本的功能。 但是键盘首先是可见的,并且向上移动动画被延迟,并且在更新到ios 11之后它有更多的延迟。

代码:

 {
    "to":"egAhXXXXXXXXI-HDeLB0SgpK5AmJ8nda1B1TNZrIV-Rpl-1Fqt15UPvfSBdYUWGQoJqaMCz29RvRELdaNE-KaiQvOP-QbmmcWMKe5Y_P7lA38NFiIl2RQHaVh5JJCtv6IId-7kRG1K76o",
  "notification" : {
    "body" : "Body one",
    "title" : "Title",
    "icon" : "beintent",
    "sound": "getsintheway",
    "color":"#ff0000"

  },
  "collapse_key":"Key 1"
}

viewDidLoad(){ NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil) } @objc func keyboardNotification(notification: NSNotification) { if let userInfo = notification.userInfo { let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue let duration:TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0 let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIViewAnimationOptions.curveEaseInOut.rawValue let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw) if (endFrame?.origin.y)! >= UIScreen.main.bounds.size.height { self.keyboardHeightLayoutConstraint?.constant = 0.0 } else { self.keyboardHeightLayoutConstraint?.constant = endFrame?.size.height ?? 0.0 } UIView.animate(withDuration: duration, delay: TimeInterval(0), options: animationCurve, animations: { self.view.layoutIfNeeded() }, completion: nil) } } 是scrollview的底部约束。

我做错了什么?任何帮助,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

你尝试这个给出scrollview的高度并用主视图高度计算 - 键盘高度和键盘更换帧将工作延迟,如果你想要打开键盘并隐藏你应该将它们添加到viewdidload:

global:var keyboardHeight:CGFloat?

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)

当加载计算scrollview高度

self.scrollviewHeight.constant = self.view.frame.height - keyboardHeight - self.textView.frame.height

你可以用UIView.animation

这样计算