局域網(wǎng)內(nèi)端到端的聊天項目(一)

局域網(wǎng)內(nèi)端到端的聊天項目(二)
局域網(wǎng)內(nèi)端到端的聊天項目(三)
局域網(wǎng)內(nèi)端到端的聊天項目(四)
局域網(wǎng)內(nèi)端到端的聊天項目(五)
局域網(wǎng)內(nèi)端到端的聊天項目(六)
局域網(wǎng)內(nèi)端到端的聊天項目(七)
局域網(wǎng)內(nèi)端到端的聊天項目(八)

前言

一.項目準(zhǔn)備實現(xiàn)的及時通訊功能功能

  • 文字/表情符的相互發(fā)送
  • 圖片/視頻的發(fā)送預(yù)覽
  • 語音發(fā)送
  • 當(dāng)前位置的發(fā)送
  • 類似QQ電話的實現(xiàn)
  • ps:以后有其他功能在添加

二.UI部分

  • 自定義鍵盤工具欄的實現(xiàn)

// 自定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;
/// 點擊發(fā)送按鈕
- (void)ESKeyBoardToolViewSendButtonDidClick:(ESKeyBoardToolView *)view message:(NSString *)message;
/// 當(dāng)正在編輯文字時view的Y值變化
- (void)ESKeyBoardToolViewDidEditing:(ESKeyBoardToolView *)view  changeY:(CGFloat)yValue;
/// 結(jié)束編輯回調(diào)
- (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

  • 監(jiān)聽UITextView 根據(jù)輸入的文字變化高度及發(fā)送等
    主要通過獲取textView的行數(shù)來計算ESKeyBoardToolView的高度同時把每次改變的值通過代理回調(diào)出去 具體實現(xiàn)如下
#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" 發(fā)送消息
    if ([textStr hasSuffix:@"\n"]) {
        [self sendMessageLayoutWithTextLines:lines message:textStr];
        return;
    }
    if (lines > maxLines) {
        textView.scrollEnabled = YES;
        _currentLine = maxLines;
        return;
    }
    textView.scrollEnabled = NO;
    
    // 當(dāng)前變化的高度
    CGFloat offsetH = self.textRowHeight;
    
    if (lines > _currentLine) {
        offsetH = (lines - _currentLine) * self.textRowHeight;
        self.hj_height = height + 10;
        self.y -= offsetH;
    }else if (lines < _currentLine){
        // 文字刪除減少的時候 減少的高度 上一次的行數(shù) - 現(xiàn)在的行數(shù)
        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
// 消息發(fā)送后重新布局
- (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
  • 表情鍵盤的實現(xiàn)

    1:對應(yīng)表情的plist文件 本地的表情圖標(biāo)
    Snip20171124_6.png

    Snip20171124_5.png
    Snip20171124_4.png
// 表情內(nèi)容view EmoticonContentView
#import <UIKit/UIKit.h>

// 一頁中最多3行
#define ESEmotionMaxRows 3
// 一行中最多7列
#define ESEmotionMaxCols 6
// 每一頁的表情個數(shù)
#define ESEmotionPageSize ((ESEmotionMaxRows * ESEmotionMaxCols) - 1)

@class EmoticonContentView;
@protocol EmoticonContentViewDelegate <NSObject>
@optional
// 點擊表情的回調(diào)
- (void)emoticonContentInsetEmoticon:(EmoticonContentView *)view insetMessage:(NSString *)message;
// 刪除表情的回調(diào)
- (void)emoticonContentDeleteEmoticon:(EmoticonContentView *)view;
@end

@interface EmoticonContentView : UIView
/// 這一頁顯示的表情(里面都是ESEmotion模型
@property (nonatomic, strong) NSArray *emotions;
/// delegate
@property (nonatomic, weak) id <EmoticonContentViewDelegate> delegate;
@end

// 外界通過 emotions 數(shù)組來賦值 最后一個是刪除按鈕
- (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];
}

// 布局對應(yīng)的表情按鈕 及刪除按鈕
- (void)layoutSubviews
{
    [super layoutSubviews];
    // 內(nèi)邊距(四周)
    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中加入內(nèi)容方法

[self.inputTextView insertText:message];

textView刪除內(nèi)容方法

[self.inputTextView deleteBackward];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容