iOS IM開發建議(三)添加一個自定義鍵盤


  各類的主流IM,都有自己定義的鍵盤:有表情鍵盤,選圖片的鍵盤。其實都是一個inputView。  

  首先,我們要確定,我們的鍵盤是輸入框調用的。也就是,我們可以設置的是某一個textView的inputView。

// 讓鍵盤進入編輯狀態,替換輸入源為自定義的fv
// fv 是一個自定義的UIView
- (void)callFaceKeyBoard:(UIButton *)button {
    [ktextView becomeFirstResponder];
    ktextView.inputView = fv;
    [ktextView reloadInputViews];
    [ktextView.inputView becomeFirstResponder];// 把自定義鍵盤設置為第一響應
}

  現在這個鍵盤是調出來了。那我們看看鍵盤怎么實現。

 1 .h
 2 #import <UIKit/UIKit.h>
 3 
 4 @interface FaceKeyboardView : UIView
 5 // 點擊了哪一個表情
 6 @property(nonatomic,copy)void (^TapActionBlock)(NSInteger faceID);
 7 // 發送信息
 8 @property(nonatomic,copy)void (^SendEmojiBlock)();
 9 
10 - (instancetype)initWithFrame:(CGRect)frame faceArray:(NSArray *)facesArray;
11 @end
12 
13 
14 
15 .m
16 
17 #import "FaceKeyboardView.h"
18 @implementation FaceKeyboardView{
19     NSMutableArray * dataArray;// 表情的資源數組
20     UIScrollView * scrollView;// 主的選折頁面
21     UIButton * sendButton;// 發送按鈕
22 }
23 // 這個一定要實現 而且要設置YES
24 - (BOOL)canBecomeFirstResponder {
25     return YES;
26 }
27 - (instancetype)initWithFrame:(CGRect)frame faceArray:(NSArray *)facesArray{
28     self = [super initWithFrame:frame];
29     if(self) {
30         //設置發送按鈕
31                code...
32         //設置主體內容
33         [self setScrollViewContent:facesArray];
34     }
35     return self;
36 }
37 
38 - (void)setScrollViewContent:(NSArray *)array {
39     //這里布置好每一個表情的view 添加點擊事件
40 }
41 
42 - (void)touchThisView:(UIGestureRecognizer *)tap {
43     if([tap.view isKindOfClass:[UIImageView class]])
44     if(self.TapActionBlock) {
45         self.TapActionBlock(tap.view.tag);
46     }
47 }
48 
49 - (void)send:(UIButton *)button {
50     if(self.SendEmojiBlock){
51         self.SendEmojiBlock();
52     }
53 }
54 
55 @end
56             

  到這里,只要實現block,把對應的東西添加到textView里面就好了。

  當然要關掉它的話,就把textView的inputView設置為nil。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM