自定義表情鍵盤(pán)-系統(tǒng)鍵盤(pán)、表情鍵盤(pán)切換邏輯

設(shè)置自定義鍵盤(pán)與系統(tǒng)鍵盤(pán)切換判斷邏輯:

composeTextView : 自定義的TextView(繼承自UITextView)
bottomToolBar : 自定義的輔助工具條(繼承自UIView)

控制器下切換鍵盤(pán):

    // 切換鍵盤(pán)方法
    func switchKeyboard () -> Void {
        
        // 系統(tǒng)鍵盤(pán)  點(diǎn)擊切換按鈕后,切換到自定義鍵盤(pán)
        if composeTextView.inputView == nil  {
            
            composeTextView.inputView = UIButton(type: UIButtonType.ContactAdd)
            
        } else {
            
            // 自定義鍵盤(pán)  點(diǎn)擊切換按鈕后,切換到系統(tǒng)鍵盤(pán)
            composeTextView.inputView = nil

        }
        // 刷新inputView
        composeTextView.reloadInputViews()
        // 成為第一響應(yīng)者
        composeTextView.becomeFirstResponder()
        
    }

自定義鍵盤(pán)(button演示):


keyboard_01.png

系統(tǒng)鍵盤(pán):

keyboard_02.png

自定義鍵盤(pán)ToolBar按鈕圖片切換:

在bottomToolBar類(lèi)中聲明一個(gè)Bool類(lèi)型的對(duì)外屬性,用來(lái)作為按鈕狀態(tài)切換的標(biāo)識(shí)


    如果isEmoticon == true  代表當(dāng)前為自定義鍵盤(pán) button 顯示image為鍵盤(pán)
    如果isEmoticon == false 代表當(dāng)前為系統(tǒng)鍵盤(pán) button 顯示image為自定義鍵盤(pán)

代碼:

    // 表情鍵盤(pán)按鈕標(biāo)識(shí) (供外界設(shè)置)
    /*
        如果isEmoticon == true  代表當(dāng)前為自定義鍵盤(pán) button顯示image為系統(tǒng)鍵盤(pán)
        如果isEmoticon == false 代表當(dāng)前為系統(tǒng)鍵盤(pán)   button顯示image為自定義鍵盤(pán)
     
     */
    var isEmoticon: Bool = false{
        didSet{
            if isEmoticon {
                
                //圖標(biāo)顯示系統(tǒng)鍵盤(pán)(當(dāng)前是自定義鍵盤(pán))
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            } else {
                
                //圖標(biāo)顯示自定義鍵盤(pán)(當(dāng)前是系統(tǒng)鍵盤(pán))
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
        }
    }
    // 表情鍵盤(pán)按鈕  因?yàn)樾枰獙?duì)齊進(jìn)行設(shè)置,所以需要抽取出來(lái)
    var emoticonButton: UIButton?

在控制器中點(diǎn)擊自定義ToolBar內(nèi)按鈕切換鍵盤(pán)時(shí),設(shè)置標(biāo)識(shí)(通過(guò)閉包監(jiān)聽(tīng)Button)

    // 切換鍵盤(pán)
    func switchKeyboard () -> Void {
        
        // 系統(tǒng)鍵盤(pán)  點(diǎn)擊切換按鈕后,切換到自定義鍵盤(pán)
        if composeTextView.inputView == nil  {
            
            composeTextView.inputView = keyboard
            bottomToolBar.isEmoticon = true
            
        } else {
            
            // 自定義鍵盤(pán)  點(diǎn)擊切換按鈕后,切換到系統(tǒng)鍵盤(pán)
            composeTextView.inputView = nil
            bottomToolBar.isEmoticon = false

        }
        // 刷新inputView
        composeTextView.reloadInputViews()
        // 成為第一響應(yīng)者
        composeTextView.becomeFirstResponder()
        
    }

顯示自定義鍵盤(pán)時(shí),圖標(biāo)顯示為系統(tǒng)鍵盤(pán)


emo.png

顯示系統(tǒng)鍵盤(pán)時(shí),圖標(biāo)顯示為自定義鍵盤(pán)

key.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容