我正在使用swift,但TouchUpInside出现问题:如果我正在使用UIKeyboardWillChangeFrame或UIKeyboardWillShow / UIKeyboardWillHide,并且显示了键盘,并且当最初显示键盘时,我要按下的按钮在键盘后面。 (如果我向下滚动到该按钮直到可见并按下,则不会调用touchUpInside。)>
TouchDown似乎可以连续显示键盘,但不会调用TouchUpInside。最初显示键盘时,如果按钮在键盘顶部上方,则TouchUpInside起作用。我正在使用keyboardNotification设置scrollView下方的视图高度,以便在显示键盘时抬起scrollView。据我所见,通常仅当按钮是scrollView中的最后一个元素时(因此,当显示键盘时,它很可能在键盘后面)。
@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") }
viewWillAppear:
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) } }
我想不使用TouchDown而关闭键盘并同时调用saveTouchUpInside。
[我正在使用swift,但TouchUpInside出现问题:如果我正在使用UIKeyboardWillChangeFrame或UIKeyboardWillShow / UIKeyboardWillHide,并且键盘正在显示,并且我正在尝试......
我将键盘交互作为一个单独的类进行抽象,这样我的控制器就不会肿(也跟随separation of concerns
)。这是我使用的键盘管理器类。