簡體   English   中英

Swift - 在用戶輸入時獲取鍵盤輸入

[英]Swift - Get keyboard input as the user is typing

我有一個 swift 應用程序,文本字段位於屏幕底部,當用戶嘗試輸入任何內容時,鍵盤會覆蓋文本字段,因此用戶無法看到他/她正在輸入的內容。 為了解決這個問題,我想從用戶那里獲取鍵盤輸入,每次筆畫/“類型”(每次用戶按下一個鍵),然后將其顯示在位於鍵盤正上方的視圖上。 如果這是不可能的,或者非常復雜,我們非常歡迎您提出替代解決方案。

您需要注冊為通知的觀察者才能查看鍵盤何時出現和消失。 然后,您可以在顯示時向上移動視圖,或在隱藏時將其恢復為原始視圖。

我的實現將整個視圖向上移動鍵盤高度,但如果您願意,您可以向上移動 UITextField。

您可以查看本教程,或者在答案中查看我的實現:

http://www.ioscreator.com/tutorials/move-view-behind-keyboard-ios8-swift

viewDidAppear添加觀察者

override func viewWillAppear(animated:Bool) {
    super.viewWillAppear(animated)

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}

然后,您需要在keyboardWillShowkeyboardWillHide新方法中移動視圖的方法。

@objc func keyboardWillShow(_ notification: NSNotification) {
    let info = notification.userInfo!
    let keyboardSize = (info[UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue

    let keyboardHeight:CGFloat = keyboardSize.height

    //let animationDuration:CGFloat = info[UIResponder.keyboardAnimationDurationUserInfoKey] as! CGFloat

    UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
        self.view.transform = CGAffineTransform(translationX: 0, y: -keyboardHeight)
        }, completion: nil)

}

並將視圖移動到原始狀態:

@objc func keyboardWillHide(_ notification: NSNotification) {
    UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
        self.view.transform = CGAffineTransform.identity
        }, completion: nil)
}

最后刪除viewDidDisappear中的觀察者

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    NotificationCenter.default.removeObserver(self)
}

為了配合 Stefan Salatics 的回答,我使用以下代碼,因為鍵盤通常仍會覆蓋文本字段。

-(void) keyboardWillShow:(NSNotification *)note
{
// Get the keyboard size
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];

// Start animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];


// Get Keyboard height and subtract the screen height by the origin of the textbox and height of text box to position textbox right above keyboard
self.scrollView.contentOffset = CGPointMake(0,keyboardBounds.size.height-([UIScreen mainScreen].bounds.size.height - commentBox.frame.origin.y - commentBox.frame.size.height));

[UIView commitAnimations];
}

以下是我如何解決該問題。 我希望這個方法能解決你的問題。

這是您要使用的文本字段。

@IBOutlet var userID : UITextField!

你需要寫這個函數。

func textFieldShouldReturn(textField: UITextField!)-> Bool
{   userID.resignFirstResponder( )
    return true
}

在您想要的函數中,您需要編寫此語法。

@IBAction func login(sender: AnyObject)
{
    userID.resignFirstResponder( )
}

這是在 ViewDidLoad 或 ViewDidAppear

override func viewDidLoad( )
{
    userID.delegate = self;
}

我希望這對你有用。

盡管 Stefan's Answer 更適合您當前的實現。 您的問題是如何獲取用戶鍵入的每個筆划。

為了完成

class viewController: UIViewController, UITextFieldDelegate{    
    var textField = UITextField()

    override func viewDidLoad() {
        textField.delegate = self
    }

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        //string is used here as the value of the textField.
        //This will be called after every user input in the textField
    }
}

string可用於查看最新的用戶輸入是什么。 您可以使用當前值存儲一個局部變量,並將其與“新”值進行比較以查看已更改的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM