最近做项目要用到聊天键盘,因为之前都没有接触过,所以就上网找了资料。但是发现别人写的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
新手发文,欢迎来喷!!