怎么在iOS中实现聊天输入框功能

  介绍

这篇文章将为大家详细讲解有关怎么在iOS中实现聊天输入框功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

实现方法如下:

安康;(空白)viewDidLoad  {   (才能super  viewDidLoad);   时间=self.view.backgroundColor 才能;[UIColor  colorWithRed: 0.92,格林:0.92,蓝色:0.92,α:1.00);   ,,   self.keyView 才能=,[[DKSKeyboardView  alloc], initWithFrame: CGRectMake (0,, K_Height 作用;51岁,K_Width,, 51)];//才能设置代理方法   时间=self.keyView.delegate 才能;自我;   (才能self.view  addSubview _keyView):;   }

主要就是上面的添加,此时输入框就已经添加到当前的VC中;稍后会讲到里面的代理方法的作用;

工程结构如下图

怎么在iOS中实现聊天输入框功能“> <强>,</强> </p> <p>主要是红色线标出的两个类,结构比较简单</p>类名作用DKSKeyboardView布局表情按钮,更多按钮,输入框DKSTextView <p>设置输入行数、输入框内容变化时改变输入款高度</p> <p> DKSKeyboardView。h中的代码如下:</p> <pre类= # import  @protocol  DKSKeyboardDelegate  @optional //非必实现的方法/* *   ,点击发送时输入框内的文案   ,@param  textStr 文案   ,*/安康;(空白)textViewContentText:(NSString  *) textStr;/* *   ,键盘的框架改变   ,*/安康;(空白)keyboardChangeFrameWithMinY:(CGFloat)如矿坑的;   @end   @interface  DKSKeyboardView : UIView  @property (原子,弱),id 委托;   @end

关于上面的两个代理方法,由于文章篇幅问题,实现的过程可参考演示,里面有详细的注释;

在DKSKeyboardView。米中,以下列出少量重要代码,主要是改变帧

1,点击输入框,键盘出现

//键盘将要出现   安康;(空白)keyboardWillShow:(NSNotification  *) notification  {   (才能self  removeBottomViewFromSupview);   NSDictionary 才能;* userInfo =, notification.userInfo;   CGRect 才能;endFrame =,(用户信息(UIKeyboardFrameEndUserInfoKey), CGRectValue);//才能获取键盘的高度   时间=self.keyboardHeight 才能;endFrame.size.height;   ,,//键才能盘的动画时长   CGFloat 才能;duration =,(用户信息(UIKeyboardAnimationDurationUserInfoKey), doubleValue);   (才能UIView  animateWithDuration: duration 延迟:0,选项:[notification.userInfo [UIKeyboardAnimationCurveUserInfoKey], integerValue],动画:^ {   ,,,self.frame =, CGRectMake (0,, endFrame.origin.y 作用;self.backView.height 作用;StatusNav_Height,, K_Width,, self.height);   ,,,(self  changeTableViewFrame);   },才能完成:nil);   }

2键盘消失

安康;(void) keyboardWillHide: (NSNotification  *) notification  {//才能如果是弹出了底部视图时   if 才能;(self.moreClick  | |, self.emojiClick), {   ,,,返回;   ,,}   (才能UIView  animateWithDuration: 0.25,动画:^ {   ,,,self.frame =, CGRectMake (0,, K_Height 作用;StatusNav_Height 作用;self.backView.height,, K_Width,, self.backView.height);   ,,,(self  changeTableViewFrame);   ,,}];   }

3点击更多按钮

安康;(void) moreBtn: (UIButton  *) btn  {   时间=self.emojiClick 才能;不,,//主要是设置表情按钮为未点击状态   if 才能;(self.moreClick ==, NO), {   ,,,self.moreClick =,是的,   ,,,//回收键盘   ,,,(self.textView  resignFirstResponder);   ,,,(self.emojiView  removeFromSuperview);   ,,,self.emojiView =,零;   ,,,(self  addSubview self.moreView):;   ,,,//改变更多,自我的框架   ,,,(UIView  animateWithDuration: 0.25,动画:^ {   ,,,,,self.moreView.frame =, CGRectMake (self.backView.height, 0,还以为,K_Width, bottomHeight);   ,,,,,self.frame =, CGRectMake (0,, K_Height 作用;StatusNav_Height 作用;self.backView.height 作用;bottomHeight,, K_Width,, self.backView.height  +, bottomHeight);   ,,,,,(self  changeTableViewFrame);   ,,,}];   ,,},else {,//再次点击更多按钮   ,,,//键盘弹起   ,,,(self.textView  becomeFirstResponder);   ,,}   }

4,改变输入框大小

安康;(void) changeFrame: (CGFloat) height  {   CGRect 才能;frame =, self.textView.frame;   时间=frame.size.height 才能;高度;   时间=self.textView.frame 才能;框架;,//改变输入框的框架//当才能输入框大小改变时,改变backView的框架   时间=self.backView.frame 才能;CGRectMake (0, 0, K_Width,, height  +, (* viewMargin  2));   null   null   null   null   null   null   null   null   null

怎么在iOS中实现聊天输入框功能