代码地址:https://github.com/Soldoros/SSChat
大家国庆节快乐呀,给伙伴们发一版聊天用的多媒体键盘库,因为项目中也要用,定制度也很高,用其他的开源库,界面和效果都难改动。封装了一些文件,就不罗列了。大家看效果图,后面有使用的代码。
1.将 SSChatKeyBoard 文件夹拖入到工程
2.plist文件需要设置权限 访问相机 麦克风 相册
3.在需要用键盘的控制器引用头文件 #import "SSChatKeyBoardInputView.h" 并设置代理 SSChatKeyBoardInputViewDelegate
4.声明对象来
//底部输入框 携带表情视图和多功能视图
@property(nonatomic,strong)SSChatKeyBoardInputView *mInputView;
5.初始化多媒体键盘
_mInputView = [SSChatKeyBoardInputView new];
_mInputView.delegate = self;
[self.view addSubview:_mInputView];
6.聊天界面通常是一个表单UITableView,这个时候需要在表单点击回调和滚动视图的滚动回调里面对键盘弹出收起做一个简单处理。
//键盘和列表视图归位
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[_mInputView SetSSChatKeyBoardInputViewEndEditing];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[_mInputView SetSSChatKeyBoardInputViewEndEditing];
}
7.在键盘的回调方法中,改变输入框高度和键盘位置的方法回调中,需要处理当前表单的frame,具体frame调整需要针对界面的布局来定,这里只对UITableView和它的父视图做个简单调整。
#pragma SSChatKeyBoardInputViewDelegate 底部输入框代理回调
//点击按钮视图frame发生变化 调整当前列表frame
-(void)SSChatKeyBoardInputViewHeight:(CGFloat)keyBoardHeight changeTime:(CGFloat)changeTime{
CGFloat height = _backViewH - keyBoardHeight;
[UIView animateWithDuration:changeTime animations:^{
self.mBackView.frame = CGRectMake(0, SafeAreaTop_Height, SCREEN_Width, height);
self.mTableView.frame = self.mBackView.bounds;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.datas.count-1 inSection:0];
[self.mTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
} completion:^(BOOL finished) {
}];
}
8.其它功能根据需求而定,文本消息在跟后台对接时只能使用字符串,布局是需要做图文混排处理,生成富文本。多功能视图简单处理了图片、视频和定位,大家可以自己拓展需要的功能,并在回调方法直接编写逻辑。
//发送文本信息
-(void)SSChatKeyBoardInputViewBtnClick:(NSString *)string;
//发送语音消息
- (void)SSChatKeyBoardInputViewBtnClick:(SSChatKeyBoardInputView *)view sendVoice:(NSData *)voice time:(NSInteger)second;
//多功能视图按钮点击回调
-(void)SSChatKeyBoardInputViewBtnClickFunction:(NSInteger)index;