局域网内端到端的聊天项目(二)
局域网内端到端的聊天项目(三)
局域网内端到端的聊天项目(四)
局域网内端到端的聊天项目(五)
局域网内端到端的聊天项目(六)
局域网内端到端的聊天项目(七)
局域网内端到端的聊天项目(八)
前言
- 最近公司项目基本不怎么忙了,有时间来写写自己的东西
- 基于以前写的局域网内文件的传输局域网内通过Socket传输文件想写一个局域网内的聊天项目
一.项目准备实现的及时通讯功能功能
- 文字/表情符的相互发送
- 图片/视频的发送预览
- 语音发送
- 当前位置的发送
- 类似QQ电话的实现
- ps:以后有其他功能在添加
二.UI部分
-
自定义键盘工具栏的实现
// 自定view ESKeyBoardToolView
#import <UIKit/UIKit.h>
/**
* 输入框最多显示多少行
*/
static NSInteger maxLines = 4;
// 输入框的高度
static CGFloat const TitleViewHeight = 44.0;
typedef NS_ENUM(NSInteger, ESKeyBoardToolView_type)
{
ESKeyBoardToolView_typeEmoticon = 0, // 表情按钮的点击
ESKeyBoardToolView_typeAdd // 加号按钮的点击
};
@class ESKeyBoardToolView;
@protocol ESKeyBoardToolViewDelegate <NSObject>
@optional
- (void)ESKeyBoardToolViewDidClick:(UIButton *)button withType:(ESKeyBoardToolView_type)type;
/// 点击发送按钮
- (void)ESKeyBoardToolViewSendButtonDidClick:(ESKeyBoardToolView *)view message:(NSString *)message;
/// 当正在编辑文字时view的Y值变化
- (void)ESKeyBoardToolViewDidEditing:(ESKeyBoardToolView *)view changeY:(CGFloat)yValue;
/// 结束编辑回调
- (void)ESKeyBoardToolViewDidEndEdit:(ESKeyBoardToolView *)view;
@end
@interface ESKeyBoardToolView : UIView
/// delegate
@property (nonatomic, weak) id <ESKeyBoardToolViewDelegate> delegate;
/// 输入框
@property (nonatomic, strong,readonly) UITextView *inputTextView;
/// 占位文字
@property (nonatomic, copy) NSString *placeTitle;
/// 是否需要显示右边的添加按钮
@property (nonatomic, assign) BOOL isNeedHiddenAddButton;
/// 是否正在切换表情键盘
@property (nonatomic, assign) BOOL isChangeEmoticon;
/// 键盘完全弹出所需的时间
@property (nonatomic, assign) CGFloat showTime;
- (void)exitKeyBoard;
- (void)showKeyBoard;
@end
- 监听UITextView 根据输入的文字变化高度及发送等
主要通过获取textView的行数来计算ESKeyBoardToolView的高度同时把每次改变的值通过代理回调出去 具体实现如下
#pragma mark - textView delegate
- (void)textViewDidChange:(UITextView *)textView
{
NSString *textStr = textView.text;
if (textStr.length) {
self.placeTitleLabel.hidden = YES;
}else{
self.placeTitleLabel.hidden = NO;
}
self.isEditing = YES;
NSInteger height = ceilf([textView sizeThatFits:CGSizeMake(textView.bounds.size.width, MAXFLOAT)].height);
NSInteger lines = height / self.textRowHeight;
// 判断最后一个字符是否为 "\n" 发送消息
if ([textStr hasSuffix:@"\n"]) {
[self sendMessageLayoutWithTextLines:lines message:textStr];
return;
}
if (lines > maxLines) {
textView.scrollEnabled = YES;
_currentLine = maxLines;
return;
}
textView.scrollEnabled = NO;
// 当前变化的高度
CGFloat offsetH = self.textRowHeight;
if (lines > _currentLine) {
offsetH = (lines - _currentLine) * self.textRowHeight;
self.hj_height = height + 10;
self.y -= offsetH;
}else if (lines < _currentLine){
// 文字删除减少的时候 减少的高度 上一次的行数 - 现在的行数
offsetH = (_currentLine - lines) * self.textRowHeight;
self.hj_height -= offsetH;
self.y += offsetH;
}
self.nowHeight = self.hj_height;
if ([self.delegate respondsToSelector:@selector(ESKeyBoardToolViewDidEditing:changeY:)] && _currentLine != lines) {
[self.delegate ESKeyBoardToolViewDidEditing:self changeY:lines > _currentLine ? (-offsetH) : (offsetH)];
}
_currentLine = lines;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"])
{
NSInteger height = ceilf([textView sizeThatFits:CGSizeMake(textView.bounds.size.width, MAXFLOAT)].height);
NSInteger lines = height / self.textRowHeight;
[self sendMessageLayoutWithTextLines:lines message:textView.text];
}
return YES;
}
#pragma mark - method
// 消息发送后重新布局
- (void)sendMessageLayoutWithTextLines:(NSInteger)lines message:(NSString *)message{
self.hj_height = TitleViewHeight;
self.nowHeight = TitleViewHeight;
self.y += self.textRowHeight * (lines - 1);
self.currentLine = 1;
self.isEditing = NO;
self.inputTextView.text = nil;
self.inputTextView.scrollEnabled = NO;
[self.inputTextView resignFirstResponder];
if ([self.delegate respondsToSelector:@selector(ESKeyBoardToolViewSendButtonDidClick: message:)]) {
[self.delegate ESKeyBoardToolViewSendButtonDidClick:self message:message];
}
}
效果图:-
表情键盘的实现
1:对应表情的plist文件 本地的表情图标
// 表情内容view EmoticonContentView
#import <UIKit/UIKit.h>
// 一页中最多3行
#define ESEmotionMaxRows 3
// 一行中最多7列
#define ESEmotionMaxCols 6
// 每一页的表情个数
#define ESEmotionPageSize ((ESEmotionMaxRows * ESEmotionMaxCols) - 1)
@class EmoticonContentView;
@protocol EmoticonContentViewDelegate <NSObject>
@optional
// 点击表情的回调
- (void)emoticonContentInsetEmoticon:(EmoticonContentView *)view insetMessage:(NSString *)message;
// 删除表情的回调
- (void)emoticonContentDeleteEmoticon:(EmoticonContentView *)view;
@end
@interface EmoticonContentView : UIView
/// 这一页显示的表情(里面都是ESEmotion模型
@property (nonatomic, strong) NSArray *emotions;
/// delegate
@property (nonatomic, weak) id <EmoticonContentViewDelegate> delegate;
@end
// 外界通过 emotions 数组来赋值 最后一个是删除按钮
- (void)setEmotions:(NSArray *)emotions
{
_emotions = emotions;
NSUInteger count = emotions.count;
ESEmotionModel *emotion = nil;
for (int i = 0; i<count; i++) {
EmoticonButton *btn = [[EmoticonButton alloc] init];
btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
btn.tag = i;
emotion = emotions[i];
UIImage *image = [UIImage imageNamed:emotion.png];
[btn setImage:image forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:32];
[btn addTarget:self action:@selector(emoticonButtonClik:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
// 最后一个加上删除按钮
[self addSubview:self.deleteButton];
}
// 布局对应的表情按钮 及删除按钮
- (void)layoutSubviews
{
[super layoutSubviews];
// 内边距(四周)
CGFloat inset = 15;
NSUInteger count = self.emotions.count;
CGFloat btnW = (self.hj_width - 2 * inset) / ESEmotionMaxCols;
CGFloat btnH = (self.hj_height - inset) / ESEmotionMaxRows;
for (int i = 0; i<count; i++) {
UIButton *btn = self.subviews[i];
btn.hj_width = btnW;
btn.hj_height = btnH;
btn.x = inset + (i % ESEmotionMaxCols) * btnW;
btn.y = inset + (i / ESEmotionMaxCols) * btnH;
}
CGFloat deleteX = self.hj_width - inset - btnW;
CGFloat deleteY = self.hj_height - btnH;
self.deleteButton.frame = CGRectMake(deleteX, deleteY, btnW, btnH);
}
在textView中加入内容方法
[self.inputTextView insertText:message];
textView删除内容方法
[self.inputTextView deleteBackward];