Swift-当使用UIKeyboardWillChangeFrame时,关闭键盘并调用TouchUpInside按钮同时



我正在使用swift,并且TouchUpInside有问题:如果我使用UIKeyboardWillChangeFrame或UIKeyboard WillShow/UIKeyboardWillHide,&键盘正在显示,&当键盘最初显示时,我试图按下的按钮在键盘后面。(如果我向下滚动到按钮直到可见并按下,则不会调用touchUpInside(。

无论键盘是否显示,TouchDown似乎都能持续工作,但TouchUpInside没有被调用。如果最初显示键盘时按钮位于键盘顶部上方,则TouchUpInside起作用。我使用keyboardNotification来设置滚动视图下方视图的高度,以便在键盘显示时提升滚动视图。据我所见,通常只有当按钮是滚动视图中的最后一个元素时(因此,当显示键盘时,按钮可能在键盘后面(。

@IBOutlet var keyboardHeightLayoutConstraint: NSLayoutConstraint?
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var saveButton: UIButton!
@IBAction func saveTouchUpInside(_ sender: UIButton) {
print("touchupinside = does not work")
}
@objc func saveTouchDown(notification:NSNotification){
print("touchdown = works")
}

视图将出现:

textField.delegate = self
NotificationCenter.default.addObserver(self,selector:#selector(self.keyboardNotification(notification:)),name: 
NSNotification.Name.UIKeyboardWillChangeFrame,object: nil)
self.saveButton.addTarget(self, action:#selector(ViewController.saveTouchDown(notification:)), for: .touchDown)
deinit {
NotificationCenter.default.removeObserver(self)
}
@objc func keyboardNotification(notification: NSNotification) {
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let endFrameY = endFrame?.origin.y ?? 0
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 endFrameY >= 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)
}
}

我想关闭键盘,同时调用saveTouchUpInside,而不使用TouchDown。

我将键盘交互抽象为一个单独的类,这样我的控制器就不会变得臃肿(也遵循separation of concerns(。这是我使用的键盘管理器类。

import UIKit

/**
*  To adjust the scroll view associated with the displayed view to accommodate
*  the display of keyboard so that the view gets adjusted accordingly without getting hidden
*/
class KeyboardManager {
private var scrollView: UIScrollView
/**
*  -parameter scrollView: ScrollView that need to be adjusted so that it does not get clipped by the presence of the keyboard
*/
init(scrollView: UIScrollView) {
self.scrollView = scrollView
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self,
selector: #selector(adjustForKeyboard),
name: UIResponder.keyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self,
selector: #selector(adjustForKeyboard),
name: UIResponder.keyboardDidChangeFrameNotification, object: nil)
}
/**
* Indicates that the on-screen keyboard is about to be presented.
*  -parameter notification: Contains animation and frame details on the keyboard
*
*/
@objc func adjustForKeyboard(notification: Notification) {
guard let containedView = scrollView.superview else { return }
let userInfo = notification.userInfo!
let keyboardScreenEndFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let keyboardViewEndFrame = containedView.convert(keyboardScreenEndFrame, to: containedView.window)
let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber
let rawAnimationCurveValue = (userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber).uintValue
UIView.animate(withDuration: TimeInterval(truncating: duration),
delay: 0,
options: [UIView.AnimationOptions(rawValue: rawAnimationCurveValue)],
animations: {
if notification.name == UIResponder.keyboardWillHideNotification {
self.scrollView.contentInset = UIEdgeInsets.zero
} else {
self.scrollView.contentInset = UIEdgeInsets(top: 0,
                                                              left: 0,
                                                              bottom: keyboardViewEndFrame.height,
                                                              right: 0)
}
self.scrollView.scrollIndicatorInsets = self.scrollView.contentInset
},
completion: nil)
}
deinit {
let notificationCenter = NotificationCenter.default
notificationCenter.removeObserver(self)
}
}

它的用途就像这个

  • 创建对键盘管理器的引用
private var keyboardManager: KeyboardManager!
  • 并在viewDidLoad中分配如下所示的键盘管理器类,其中self.scrollView是您正在使用的滚动视图
self.keyboardManager = KeyboardManager(scrollView: self.scrollView)

这应该能解决这个问题。如果这不起作用,那么一个示例项目可能有助于深入研究。

最新更新