自定义表情键盘-系统键盘、表情键盘切换逻辑

设置自定义键盘与系统键盘切换判断逻辑:

composeTextView : 自定义的TextView(继承自UITextView)
bottomToolBar : 自定义的辅助工具条(继承自UIView)

控制器下切换键盘:

    // 切换键盘方法
    func switchKeyboard () -> Void {
        
        // 系统键盘  点击切换按钮后,切换到自定义键盘
        if composeTextView.inputView == nil  {
            
            composeTextView.inputView = UIButton(type: UIButtonType.ContactAdd)
            
        } else {
            
            // 自定义键盘  点击切换按钮后,切换到系统键盘
            composeTextView.inputView = nil

        }
        // 刷新inputView
        composeTextView.reloadInputViews()
        // 成为第一响应者
        composeTextView.becomeFirstResponder()
        
    }

自定义键盘(button演示):


自定义表情键盘-系统键盘、表情键盘切换逻辑_第1张图片
keyboard_01.png

系统键盘:

自定义表情键盘-系统键盘、表情键盘切换逻辑_第2张图片
keyboard_02.png

自定义键盘ToolBar按钮图片切换:

在bottomToolBar类中声明一个Bool类型的对外属性,用来作为按钮状态切换的标识


    如果isEmoticon == true  代表当前为自定义键盘 button 显示image为键盘
    如果isEmoticon == false 代表当前为系统键盘 button 显示image为自定义键盘

代码:

    // 表情键盘按钮标识 (供外界设置)
    /*
        如果isEmoticon == true  代表当前为自定义键盘 button显示image为系统键盘
        如果isEmoticon == false 代表当前为系统键盘   button显示image为自定义键盘
     
     */
    var isEmoticon: Bool = false{
        didSet{
            if isEmoticon {
                
                //图标显示系统键盘(当前是自定义键盘)
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            } else {
                
                //图标显示自定义键盘(当前是系统键盘)
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
        }
    }
    // 表情键盘按钮  因为需要对齐进行设置,所以需要抽取出来
    var emoticonButton: UIButton?

在控制器中点击自定义ToolBar内按钮切换键盘时,设置标识(通过闭包监听Button)

    // 切换键盘
    func switchKeyboard () -> Void {
        
        // 系统键盘  点击切换按钮后,切换到自定义键盘
        if composeTextView.inputView == nil  {
            
            composeTextView.inputView = keyboard
            bottomToolBar.isEmoticon = true
            
        } else {
            
            // 自定义键盘  点击切换按钮后,切换到系统键盘
            composeTextView.inputView = nil
            bottomToolBar.isEmoticon = false

        }
        // 刷新inputView
        composeTextView.reloadInputViews()
        // 成为第一响应者
        composeTextView.becomeFirstResponder()
        
    }

显示自定义键盘时,图标显示为系统键盘


自定义表情键盘-系统键盘、表情键盘切换逻辑_第3张图片
emo.png

显示系统键盘时,图标显示为自定义键盘

自定义表情键盘-系统键盘、表情键盘切换逻辑_第4张图片
key.png

你可能感兴趣的:(自定义表情键盘-系统键盘、表情键盘切换逻辑)