局域网内端到端的聊天项目(一)

局域网内端到端的聊天项目(二)
局域网内端到端的聊天项目(三)
局域网内端到端的聊天项目(四)
局域网内端到端的聊天项目(五)
局域网内端到端的聊天项目(六)
局域网内端到端的聊天项目(七)
局域网内端到端的聊天项目(八)

前言

  • 最近公司项目基本不怎么忙了,有时间来写写自己的东西
  • 基于以前写的局域网内文件的传输局域网内通过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];
    }
}

效果图:
IMG_2407.GIF
  • 表情键盘的实现

    1:对应表情的plist文件 本地的表情图标
    Snip20171124_6.png

    Snip20171124_5.png
    Snip20171124_4.png
// 表情内容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];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335