gut*_*ago 5 keyboard scroll dismiss ios swift
我正在创建一个聊天界面,就像WhatsApp一样,我创建了一个"scrollToBottom"按钮,当用户以一定距离滚动集合时会出现该按钮.当键盘出现时,此按钮完全跟随键盘框架,当消失时,唯一的问题是当键盘被交互式解除时,我无法使该按钮跟随键盘框架.只有在隐藏键盘后,系统才会发送通知并且按钮会更改其常量.
我已经尝试了所有键盘通知,但没有一个能帮我解决这个问题.我需要一些及时的东西,使按钮跟随键盘没有任何延迟.甚至UIKeyboardWillChangeFrameNotification没有为我工作过.
NSNotificationCenter.defaultCenter().addObserver(self,
selector:#selector(self.keyboardWillShow(_:)),
name:UIKeyboardWillShowNotification,
object:nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector:#selector(self.keyboardWillHide(_:)),
name:UIKeyboardWillHideNotification,
object:nil)
private func configureConstantViewNewMessages(notification: NSNotification){
if let userInfo = notification.userInfo {
let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
let keyboardEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let convertedKeyboardEndFrame = view.convertRect(keyboardEndFrame, fromView: view.window)
let rawAnimationCurve = (notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).unsignedIntValue << 16
let animationCurve = UIViewAnimationOptions(rawValue: UInt(rawAnimationCurve))
self.kNewMessages.constant = CGRectGetMaxY(view.bounds) - CGRectGetMinY(convertedKeyboardEndFrame) + 10
UIView.animateWithDuration(animationDuration, delay: 0.0, options: [.BeginFromCurrentState, animationCurve], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,我调用该方法configureConstantViewNewMessages来设置我的按钮常量(kNewMessages)的动画,它可以根据键盘高度更改其位置.
感谢您对英语错误的支持和抱歉.