天天看點

自定義聊天鍵盤

最近做項目要用到聊天鍵盤,因為之前都沒有接觸過,是以就上網找了資料。但是發現别人寫的demo都很複雜(我隻是要表情、加檔案、輸入框互相點選都可以),糾結了2-3天,萬萬沒想到還是自己寫了出來。(雖然感覺寫得不怎麼樣,但是還是分享下,讓大家給點意見)

.h檔案

#import "BSViewController.h"
#define kDeviceWidth [UIScreen mainScreen].bounds.size.width        //螢幕寬
#define KDeviceHeight [UIScreen mainScreen].bounds.size.height      //螢幕高
#define kTabbarHeight 50                                            //tabbaer高度

 @interface ChatRoomViewController : BSViewController<UITableViewDelegate,UITextViewDelegate>
{
    UITableView *ChatRoomTableView;
    
    UIView *bottom_view;//底部工具欄
    UIView *expressionView;//表情鍵盤
    UIView *addView;//加檔案功能view
    UIButton *expressionBtn;//表情按鈕
    UIButton *addBtn;//加号按鈕
    UITextView *chatTF;//聊天輸入框
    UITapGestureRecognizer *tap_back;//收回鍵盤手勢
}
@property(nonatomic , assign)BOOL isKey;//yes 鍵盤彈出   no 鍵盤消失
@property(nonatomic,assign)BOOL isExpression;//yes 表情框彈出 no表情框消失
@property(nonatomic,assign)BOOL isAdd;//yes 增加檔案框彈出 no增加檔案框消失

-(void)moveTableView_tabbar_Height:(CGFloat)height;
@end
           

.m檔案

#import "ChatRoomViewController.h"
@interface ChatRoomViewController ()

@end

@implementation ChatRoomViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self HttpRequest_ChatLoginRoom];
    [self HttpRequest_ChatGetMsg];
    [self ChatRoomTableView_alloc];
    [self bottomView_alloc];
    
    _isKey = NO;
    _isExpression = NO;
    _isAdd = NO;
    
    //注冊一個通知,擷取鍵盤的高度
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShowNotification:) name:UIKeyboardWillShowNotification object:nil];
    
    //注冊一個通知,隐藏鍵盤高度
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardHiddenNotification:) name:UIKeyboardWillHideNotification object:nil];
    
}

-(void)keyboardShowNotification:(NSNotification *)notification
{
    [UIView beginAnimations:@"key" context:nil];
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    if (_isExpression == YES) {
        _isExpression = NO;
        [self moveTableView_tabbar_Height:0];
    }
    
    if (_isAdd == YES) {
        _isAdd = NO;
        [self moveTableView_tabbar_Height:0];
    }
    
    
    if (_isKey == NO) {
        _isKey = YES;
        [self moveTableView_tabbar_Height:-253];
    }
    
    [UIView commitAnimations];
    
    
}
-(void)KeyboardHiddenNotification:(NSNotification *)notification{
    
    [UIView beginAnimations:@"key" context:nil];
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    if (_isKey == YES) {
        _isKey = NO;
        [self moveTableView_tabbar_Height:0];
    }
    
    
    [UIView commitAnimations];
    
}

#pragma mark - 回收鍵盤
- (void)touchBk
{
    [chatTF resignFirstResponder];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self touchBk];
}

-(void)bottomView_alloc
{
    bottom_view = [[UIView alloc]initWithFrame:CGRectMake(0, KDeviceHeight-kTabbarHeight, kDeviceWidth, kTabbarHeight)];
    bottom_view.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:bottom_view];
    
    //表情按鈕
    expressionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [expressionBtn setImage:[UIImage imageNamed:@"emoji"] forState:0];
    expressionBtn.frame = CGRectMake(10, 5, 40, 40);
    [expressionBtn addTarget:self action:@selector(expressionActionAlloc:) forControlEvents:UIControlEventTouchUpInside];
    [bottom_view addSubview:expressionBtn];
    
    //加号按鈕
    addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setImage:[UIImage imageNamed:@"addicon"] forState:0];
    addBtn.frame = CGRectMake(50, 5, 40, 40);
    [addBtn addTarget:self action:@selector(addActionAlloc:) forControlEvents:UIControlEventTouchUpInside];
    [bottom_view addSubview:addBtn];
    
    //聊天輸入框
    chatTF = [[UITextView alloc]initWithFrame:CGRectMake(90, 5, 160, 40)];
    chatTF.layer.borderColor =  [[UIColor orangeColor] CGColor];
    chatTF.backgroundColor = [UIColor whiteColor];
    chatTF.delegate = self;
    chatTF.font = [UIFont systemFontOfSize:17];
    [bottom_view addSubview:chatTF];
    
    //發送按鈕
    UIButton *senderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    senderBtn.frame = CGRectMake(270, 5, 40, 40);
    [senderBtn setTitle:@"發送" forState:0];
    [senderBtn addTarget:self action:@selector(sendeAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottom_view addSubview:senderBtn];
    
}

#pragma mark----移動tableview和tabbar的高度,彈出表情鍵盤、發送檔案
-(void)moveTableView_tabbar_Height:(CGFloat)height
{
    bottom_view.frame = CGRectMake(0, KDeviceHeight-kTabbarHeight+height, kDeviceWidth, kTabbarHeight);
    ChatRoomTableView.frame = CGRectMake(0, height, kDeviceWidth, KDeviceHeight);
    
    //鍵盤回收
    if (_isKey == YES) {
        if (tap_back) {
            [self.view removeGestureRecognizer:tap_back];
        }
        tap_back = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchBk)];
        [self.view addGestureRecognizer:tap_back];
        [chatTF becomeFirstResponder];
    }else{
        if (tap_back) {
            [self.view removeGestureRecognizer:tap_back];
        }
        [chatTF resignFirstResponder];
    }
    
    
    
    
    //表情鍵盤
    if (_isExpression == YES) {
        expressionView = [[UIView alloc]initWithFrame:CGRectMake(0, KDeviceHeight-172, kDeviceWidth, 172)];
        expressionView.backgroundColor = [UIColor grayColor];
        [self.view addSubview:expressionView];
        
        NSArray *FaceNameArr = [[NSArray alloc]initWithObjects:@"bq01.gif",@"bq02.gif",@"bq03.gif",@"bq04.gif",@"bq09.gif",@"bq10.gif",@"bq13.gif",@"bq14.gif",@"bq15.gif",@"bq17.gif",@"bq18.gif",@"bq19.gif",@"bq20.gif",@"bq21.gif",@"bq22.gif",@"bq23.gif",@"bq24.gif",@"bq26.gif",@"bq27.gif",@"bq28.gif",@"back_sel", nil];
        for (int i = 0; i < 3; i++) {
            for (int y = 0; y < 7; y++) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                btn.frame = CGRectMake(12/8*(y+1)+44*y, 10*(i+1)+44*i, 44, 44);
                [btn setImage:[UIImage imageNamed:[FaceNameArr objectAtIndex:y+i*7]] forState:0];
                btn.tag = uFaceKeyboradTag + y+i*7;
                [btn addTarget:self action:@selector(faceAction:) forControlEvents:UIControlEventTouchUpInside];
                [expressionView addSubview:btn];
            }
        }
    }else{
        [expressionView removeFromSuperview];
    }
    
    
    //發送檔案
    if (_isAdd == YES) {
        addView = [[UIView alloc]initWithFrame:CGRectMake(0, KDeviceHeight-100, kDeviceWidth, 100)];
        addView.backgroundColor = [UIColor grayColor];
        [self.view addSubview:addView];
        
        NSArray *SendFileArr = [[NSArray alloc]initWithObjects:@"icon_c_6",@"icon_3",@"icon_c_13", nil];
        NSArray *SendFileNameArr = [[NSArray alloc]initWithObjects:@"檔案傳送",@"發送圖檔",@"會議記錄", nil];
        for (int i = 0; i<3; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(20*(i+1)+50*i, 20, 50, 50);
            [btn setImage:[UIImage imageNamed:[SendFileArr objectAtIndex:i]] forState:0];
            [btn addTarget:self action:@selector(sendfileAction:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = uSendFileTag + i;
            [addView addSubview:btn];
            
            
            UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(20*(i+1)+50*i, 70, 50, 20)];
            name.textAlignment = NSTextAlignmentCenter;
            name.text = [SendFileNameArr objectAtIndex:i];
            name.font = [UIFont systemFontOfSize:11];
            [addView addSubview:name];
        }
        
        
    }else{
        [addView removeFromSuperview];
    }
    
    
}

-(void)expressionActionAlloc:(UIButton *)sender
{
    if (_isKey == YES) {
        _isKey = NO;
        [self moveTableView_tabbar_Height:0];
    }
    
    if (_isAdd == YES) {
        _isAdd =  NO;
        [self moveTableView_tabbar_Height:0];
    }
    
    if (_isExpression == NO) {
        _isExpression = YES;
        [self moveTableView_tabbar_Height:-172];
    }else{
        _isExpression = NO;
        [self moveTableView_tabbar_Height:0];
    }
}

#pragma mark----點選表情的方法
-(void)faceAction:(UIButton *)sender
{
    
}


-(void)addActionAlloc:(UIButton *)sender
{
    if (_isKey == YES) {
        _isKey = NO;
        [self moveTableView_tabbar_Height:0];
    }
    
    if (_isExpression == YES) {
        _isExpression = NO;
        [self moveTableView_tabbar_Height:0];
    }
    
    if (_isAdd == NO) {
        _isAdd = YES;
        [self moveTableView_tabbar_Height:-100];
    }else{
        _isAdd = NO;
        [self moveTableView_tabbar_Height:0];
    }
}

#pragma mark----點選發送檔案的方法
-(void)sendfileAction:(UIButton *)sender
{
    
}


-(void)sendeAction:(UIButton *)sender
{
    [self HttpRequest_ChatPostMsg];
}

-(void)ChatRoomTableView_alloc
{
    ChatRoomTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth, KDeviceHeight)];
    ChatRoomTableView.delegate = self;
    ChatRoomTableView.dataSource = self;
    ChatRoomTableView.backgroundColor = [UIColor whiteColor];
    [ChatRoomTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:ChatRoomTableView];
}


@end
           

新手發文,歡迎來噴!!