从聊天界面入手,而聊天界面先从键盘的实现上入手,键盘的实现分为界面UI的实现和在视图控制器上如何调用

必须掌握的知识点有:委托协议delegate

首先定义协议,这个协议代表着在聊天界面视图控制器点击输入框触发的事件的页面传值:

#import <UIKit/UIKit.h>@class KeyBordView;@protocol KeyBordVIewDelegate <NSObject>- (void)beginRecord;
- (void)finishRecord;
- (void)KeyBordView:(KeyBordView* )keyBoardView textFiledBegin:(UITextField *)textFiled;
- (void)KeyBordView:(KeyBordView* )keyBoardView textFiledReturn:(UITextField *)textFiled;@end

键盘的UI实现(用Masonry实现),但是语音,添加表情,图片等功能还未实现

.h

#import <UIKit/UIKit.h>
#import "KeyBordVIewDelegate.h"@interface KeyBordView : UIView@property (nonatomic, assign) id <KeyBordVIewDelegate> delegate;@end

.m

#import "Masonry.h"
#import "QQChating.h"
#import "KeyBordView.h"
#import "UIImage+StrethImage.h"@interface KeyBordView () <UITextFieldDelegate>@property (nonatomic, strong) UIButton* voiceButton;
@property (nonatomic, strong) UIButton* imageButton;
@property (nonatomic, strong) UIButton* addButton;
@property (nonatomic, strong) UIButton* speakButton;
@property (nonatomic, strong) UITextField* textFiled;
@property (nonatomic, strong) UIImageView* backImageView;@end@implementation KeyBordView- (id)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self){[self setBackgroundColor:[UIColor blackColor]];[self initialData];}return self;
}#pragma mark - 初始化通UIButton- (UIButton*)buttonWithNormal:(NSString* )normal hightLight:(NSString* )hightLightaction:(SEL)action
{UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];[btn setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];[btn setImage:[UIImage imageNamed:hightLight] forState:UIControlStateHighlighted];[btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];return btn;
}#pragma mark - 初始化数据- (void)initialData
{UIView* superview = self;self.backImageView = [UIImageView new];[self.backImageView setBackgroundColor:[UIColor blackColor]];[superview addSubview:self.backImageView];self.voiceButton = [self buttonWithNormal:@"chat_bottom_voice_nor.png"hightLight:@"chat_bottom_voice_press.png"action:@selector(voiceBtnPress:)];[superview addSubview:self.voiceButton];self.textFiled = [UITextField new];self.textFiled.returnKeyType = UIReturnKeySend;self.textFiled.font = [UIFont fontWithName:@"HelveticaNeue" size:14];self.textFiled.placeholder = @"请输入";self.textFiled.background = [UIImage imageNamed:@"chat_bottom_textfield.png"];self.textFiled.delegate = self;[superview addSubview:self.textFiled];self.imageButton = [self buttonWithNormal:@"chat_bottom_smile_nor.png"hightLight:@"chat_bottom_smile_press.png"action:@selector(imageBtnPress:)];[self addSubview:self.imageButton];self.addButton = [self buttonWithNormal:@"chat_bottom_up_nor.png"hightLight:@"chat_bottom_up_press.png"action:@selector(addBtnPress:)];[self addSubview:self.addButton];self.speakButton = [self buttonWithNormal:nil hightLight:nil action:@selector(speakBtnPress:)];[self.speakButton setTitle:@"按住说话" forState:UIControlStateNormal];[self.speakButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];[self.speakButton addTarget:self action:@selector(touchDown:)forControlEvents:UIControlEventTouchDown];[self.speakButton setTitleColor:[UIColor redColor]forState:(UIControlState)UIControlEventTouchDown];[self.speakButton setBackgroundColor:[UIColor whiteColor]];self.speakButton.hidden = YES;[self addSubview:self.speakButton];[self.backImageView mas_makeConstraints:^(MASConstraintMaker* make){make.size.mas_equalTo(superview);make.center.mas_equalTo(superview);}];[self.voiceButton mas_makeConstraints:^(MASConstraintMaker* make){make.centerY.mas_equalTo(superview.mas_centerY);make.left.mas_equalTo(superview.mas_left).with.offset(10);make.width.equalTo(@33);make.height.equalTo(@33);}];[self.textFiled mas_makeConstraints:^(MASConstraintMaker* make){make.centerY.mas_equalTo(superview);make.left.mas_equalTo(self.voiceButton.mas_right).with.offset(10);make.right.mas_equalTo(self.imageButton.mas_left).with.offset(-10);make.top.mas_equalTo(superview.mas_top).with.offset(5);make.bottom.mas_equalTo(superview.mas_bottom).with.offset(-5);}];[self.imageButton mas_makeConstraints:^(MASConstraintMaker* make){make.centerY.mas_equalTo(superview);make.left.mas_equalTo(self.textFiled.mas_right).with.offset(10);make.right.mas_equalTo(self.addButton.mas_left).with.offset(-10);make.width.equalTo(@33);make.height.equalTo(@33);}];[self.addButton mas_makeConstraints:^(MASConstraintMaker* make){make.centerY.mas_equalTo(superview);make.right.mas_equalTo(superview.mas_right).with.offset(-10);make.width.equalTo(@33);make.height.equalTo(@33);}];[self.speakButton mas_makeConstraints:^(MASConstraintMaker* make){make.center.mas_equalTo(self.textFiled);make.size.mas_equalTo(self.textFiled);}];}- (void)updateConstraints
{[super updateConstraints];
}#pragma mark - 语音相关
//用的是替换方式,太low了。但是也要掌握
- (void)voiceBtnPress:(UIButton*)voiceButton
{NSString* normal = NULL;NSString* hightLight = NULL;if (self.speakButton.hidden == YES){self.speakButton.hidden = NO;self.textFiled.hidden   = YES;normal = @"chat_bottom_keyboard_nor.png";hightLight = @"chat_bottom_keyboard_press.png";}else{self.speakButton.hidden = YES;self.textFiled.hidden   = NO;normal = @"chat_bottom_voice_nor.png";hightLight = @"chat_bottom_voice_press.png";}[voiceButton setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];[voiceButton setImage:[UIImage imageNamed:hightLight] forState:UIControlStateHighlighted];
}- (void)touchDown:(UIButton*)voiceButton
{//开始录音}- (void)speakBtnPress:(UIButton*)voiceButton
{//结束录音
}#pragma mark - 表情相关- (void)imageBtnPress:(UIButton*)imageButton
{}#pragma mark - 添加功能相关- (void)addBtnPress:(UIButton*)addButton
{}#pragma mark - UITextField代理方法- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{if ([self.delegate respondsToSelector:@selector(KeyBordView:textFiledBegin:)]){[self.delegate KeyBordView:self textFiledBegin:textField];}return YES;
}- (BOOL)textFieldShouldReturn:(UITextField *)textField
{if ([self.delegate respondsToSelector:@selector(KeyBordView:textFiledReturn:)]){[self.delegate KeyBordView:self textFiledReturn:textField];}return YES;
}@end

视图控制器使用键盘,因为初创模版,所以先用了UIScrollView来测试:

#import "Masonry.h"
#import "QQChating.h"
#import "KeyBordView.h"
#import "ShowKeyboradViewController.h"@interface ShowKeyboradViewController () <KeyBordVIewDelegate, UIScrollViewDelegate>@property (nonatomic, strong) KeyBordView* keyBordView;@end@implementation ShowKeyboradViewController- (void)viewDidLoad
{[super viewDidLoad];[self.view setBackgroundColor:[UIColor whiteColor]];UIView* superview = self.view;UIScrollView* scrollView = [UIScrollView new];scrollView.backgroundColor = [UIColor lightGrayColor];scrollView.delegate = self;scrollView.contentSize = CGSizeMake(scrollView.bounds.size.width, 800);[scrollView flashScrollIndicators];[self.view addSubview:scrollView];self.keyBordView = [KeyBordView new];self.keyBordView.delegate = self;[self.view addSubview:self.keyBordView];[self.keyBordView mas_makeConstraints:^(MASConstraintMaker* make){make.width.mas_equalTo(superview.mas_width);make.height.mas_equalTo(@44);make.centerX.mas_equalTo(superview);make.bottom.mas_equalTo(superview.mas_bottom);}];[scrollView mas_makeConstraints:^(MASConstraintMaker* make){make.edges.equalTo(superview).with.insets(UIEdgeInsetsMake(0, 0, -44, 0));}];
}- (void)viewWillAppear:(BOOL)animated
{[super viewWillAppear:animated];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotification object:nil];
}- (void)viewDidDisappear:(BOOL)animated
{[super viewDidDisappear:animated];[[NSNotificationCenter defaultCenter] removeObserver:self];
}#pragma mark - NSNotificationCenter Operation- (void)keyboardWillShow:(NSNotification* )noti
{CGRect keyBoardRect = [noti.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];CGFloat deltaY = keyBoardRect.size.height;[UIView animateWithDuration:[noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{self.view.transform = CGAffineTransformMakeTranslation(0, -deltaY);}];}- (void)keyboardWillHide:(NSNotification* )noti
{[UIView animateWithDuration:[noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{self.view.transform = CGAffineTransformIdentity;}];
}#pragma mark - KeyBordViewDelegate- (void)beginRecord
{}- (void)finishRecord
{}- (void)KeyBordView:(KeyBordView *)keyBoardView textFiledBegin:(UITextField *)textFiled
{}- (void)KeyBordView:(KeyBordView *)keyBoardView textFiledReturn:(UITextField *)textFiled
{}#pragma mark - UIScrollViewDelegate- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{[self.view endEditing:YES];
}@end

下一篇章会详细讲这里的重点和注意的地方,并分析技巧,以后的学习篇章也是如此,先放上成果,再分析其实现。

imitate wechat - 1相关推荐

  1. 广东东软学院安卓实验报告二:“编写聊天APP界面”

    Android schoolwork entertainment app: imitating wechat Software tips: for learning only. The phone n ...

  2. 苹果开源代码中惊现“wechat”,老外注释的吐槽亮了!

    点击上方蓝色"方志朋",选择"设为星标"回复"666"获取独家整理的学习资料! 每个科技大厂的开源项目,几乎都是各领域开发者最重要的研究学习 ...

  3. Ubuntu14.04下安装wechat(微信)

    安装步骤 无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家.教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家.点这里可以跳转到教程 https://www ...

  4. 微信(WeChat)电脑端多开

    0x00 前言 不知道大家有没有多个微信号,我反正有一两三个. 现在电脑端微信使用频率也比较高,主要用于大文件传输,或者手机电脑文件互传等等,除了不能收红包和看朋友圈,貌似电脑端没其他毛病. 哦,还有 ...

  5. Android wechat 分享

    本篇简单介绍Android App中接入wechat分享流程. wechat分享 微信开放平台 1.1 在微信开放平台申请成为开发者 微信开放平台 2.2 创建移动应用 -> 创建成功以后(七天 ...

  6. Zabbix-3.0.3实现微信(WeChat)告警

    Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信息推送到接收人,方便告警的及 ...

  7. WeChat:微信小程序设计流程注册完善、设计开发、审核发布之详细攻略

    WeChat:微信小程序设计流程注册&完善.设计&开发.审核&发布之详细攻略 目录 微信小程序设计流程 1.注册 2.小程序信息完善 3.开发小程序 3.1.开发文档 3.2. ...

  8. WeChat之小工具:基于C++程序代码设计的查看微信撤销、撤回消息(包括文本、图片、视频等)GUI小工具

    WeChat之小工具:基于C++程序代码设计的查看微信撤销.撤回消息(包括文本.图片.视频等)GUI小工具 导读      哈哈,千万不要给程序猿随便发信息,程序猿认真起来,别说你发的微信信息,就连你 ...

  9. why wechat is not a good place for the learning, but csdn is

    it is because there are so many built in functions available at the system however, the wechat, espe ...

  10. 使用docker运行微信wechat的安装脚本

    docker运行wechat,只需要运行下面的脚本即可 #!/usr/bin/env bash # # dochat.sh - Docker WeChat for Linux # # Author: ...

最新文章

  1. [ZT]经典月薪4000过日子
  2. 引用文献管理软件Mendeley
  3. 2020胡润全球80后白手起家富豪榜:前三名两位中国人
  4. C++中缀表达式求值(中缀表达式转后缀表达式)
  5. jquery的层次选择器
  6. 原 IntelliJ IDEA 中 右键新建时,选项没有Java class的解决方法和具体解释
  7. python list转json对象_将列表转换为json对象
  8. java聊天室设计_JAVA网络聊天室的设计与实现
  9. 电子元器件检测与维修从入门到精通视频教程
  10. 品质qc工程图_QC工程图 (品管)
  11. 你还不知道web自动化测试是什么吧?今天让你发现新大陆
  12. 推荐10本大数据领域必读的经典好书(火速收藏)
  13. apktool java_apktool的使用
  14. 谷歌广告联盟(Google Adsense)通过网站获利功能在线创收
  15. JVM?干就完了!(一) - hello world
  16. 计算机与控制工程学院用英语怎么说,计算机与控制工程学院副院长童向荣老师2017年开学典礼致辞...
  17. 驱动仿真物品移动乌龟\Gazebo\cmd_vel和/gazebo/set_model_state
  18. windows7下使用mingw和msys编译JEPG源代码
  19. C#零基础入门运动控制学习教程(3)--定长加减速运动实验
  20. 牛客小白月赛21 I I love you(dp的优化)

热门文章

  1. 计算机5级什么水平考试,怎么选择2015计算机等级考试级别
  2. 腾讯云如何申请免费服务器试用
  3. [CF364D]Ghd
  4. css使用clac()垂直居中
  5. c# python3_从C#到Python —— 3 函数及函数编程
  6. windows10桌面计算机图标删除吗,win10系统桌面图标无法删除的解决方案
  7. java开发最难的地方是什么_java难在哪里?
  8. audioread函数未定义_为什么运行时提示未定义函数或变量 'wavread'。
  9. 中标麒麟linux模拟器,Kydroid安卓运行环境
  10. 元宇宙产业化还很遥远