JY-CONTENTS

JY

JY-CONTENTS
search

+

MENU

【Swift/ios】キーボードイベント

【Swift/ios】キーボードイベント

(DATE)

-

2018.03.04

(CATEGORY)

-

iosでのイベント操作はデリゲートを使用していることが多いのですが、
キーボードのイベントはNotificationCenterクラスの通知センター(オブザーバ)を利用して受け取ります。

NotificationCenter

NotificationCenter.default

NotificationCenter.defaultプロパティで通知センターへの参照を取得します。

NotificationCenter.default.addObserver

addObserver()メソッドを使って通知を受けたいイベント名(通知名)と、イベント発生時に実行するメソッド(イベントハンドラ)を通知センター(NotificationCenter.default)に登録します。

イベント名(通知名)とイベントハンドラ

動き(変化)イベント名イベントハンドラ
キーボードのframeが変化した時UIKeyboardDidChangeFrameNotification型を引数にもち
objective-cから参照可能な
任意のメソッド
キーボードが登場した時UIKeyboardWillShow””
キーボードが退場した時UIKeyboardDidHide””

※Observerについて詳しくは下記をご覧ください。
デザインパターン(Observer)

import UIKit

class ViewController: UIViewController {
   
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //デフォルトの通知センターを取得
        let notification = NotificationCenter.default
        
        //-----▼通知センターに登録---------------------
        //キーボードのframeが変化した時
        notification.addObserver(self,
                                 selector: #selector(keyboardChangeFrame(_:)),
                                 name: NSNotification.Name.UIKeyboardDidChangeFrame, object: nil)
        
        //キーボードが登場した時
        notification.addObserver(self,
                                 selector: #selector(keyboardWillShow(_:)),
                                 name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        

        // キーボードが退場した時
        notification.addObserver(self,
                                 selector: #selector(keyboardDidHide(_:)),
                                 name: NSNotification.Name.UIKeyboardDidHide, object: nil)
        
    }

    
    // キーボードのframeが変化した通知を受けた時に実行されるメソッド(任意)
    @objc func keyboardChangeFrame(_ notification: Notification) {
        print("keyboardChangeFrame")
    }
    
    // キーボードが登場する通知を受けた時に実行されるメソッド(任意)
    @objc func keyboardWillShow(_ notification: Notification) {
        print("keyboardWillShow")
    }
    
    // キーボードが退場した通知を受けた時に実行されるメソッド(任意)
    @objc func keyboardDidHide(_ notification: Notification) {
       print("keyboardDidHide")
    }

 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

任意メソッドの引数

通知時に呼び出される任意メソッドの引数のnotificationには通知に関する情報やキーボードに関する情報が渡されています。
キーボードに関する情報はuserInfoプロパティで取得できます。

let userInfo = (notification as NSNotification).userInfo!

/*
userInfoの中身
[
AnyHashable("UIKeyboardCenterBeginUserInfoKey"): NSPoint: {160, 568}, 
AnyHashable("UIKeyboardIsLocalUserInfoKey"): 1, 
AnyHashable("UIKeyboardCenterEndUserInfoKey"): NSPoint: {160, 568}, 
AnyHashable("UIKeyboardBoundsUserInfoKey"): NSRect: {{0, 0}, {320, 0}}, 
AnyHashable("UIKeyboardFrameEndUserInfoKey"): NSRect: {{0, 568}, {320, 0}}, 
AnyHashable("UIKeyboardAnimationCurveUserInfoKey"): 7, 
AnyHashable("UIKeyboardFrameBeginUserInfoKey"): NSRect: {{0, 568}, {320, 0}}, 
AnyHashable("UIKeyboardAnimationDurationUserInfoKey"): 0.25
]
*/

中身はディクショナリー(ハッシュ)になっています。
例えばキーボードのframeの値を取得する場合は下記のようになります。

userInfo[UIKeyboardFrameEndUserInfoKey]

//取得値
//NSRect: {{0, 568}, {320, 0}}

frameの形式で取得する場合はNSValue型にキャストしてcgRectValueを参照します。

let keybordFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue

//keybordFrameの中身
//(0.0, 568.0, 320.0, 0.0)

NEW TOPICS

/ ニュー & アップデート

SEE ALSO

/ 似た記事を見る

JY CONTENTS UNIQUE BLOG

search-menu search-menu