bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

怎么在iOS中實現聊天輸入框功能

這篇文章將為大家詳細講解有關怎么在iOS中實現聊天輸入框功能,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

浙江網站建設公司成都創新互聯公司,浙江網站設計制作,有大型網站制作公司豐富經驗。已為浙江上千多家提供企業網站建設服務。企業網站搭建\成都外貿網站制作要多少錢,請找那個售后服務好的浙江做網站的公司定做!

實現方法如下:

- (void)viewDidLoad {
  [super viewDidLoad];
  self.view.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1.00];
  
  self.keyView = [[DKSKeyboardView alloc] initWithFrame:CGRectMake(0, K_Height - 51, K_Width, 51)];
  //設置代理方法
  self.keyView.delegate = self;
  [self.view addSubview:_keyView];
}

主要就是上面的添加,此時輸入框就已經添加到當前的VC中;稍后會講到里面的代理方法的作用;

工程結構如下圖

怎么在iOS中實現聊天輸入框功能 

主要是紅色線標出的兩個類,結構比較簡單

類名作用
DKSKeyboardView布局表情按鈕、更多按鈕、輸入框
DKSTextView

設置輸入行數,輸入框內容變化時改變輸入款高度

DKSKeyboardView.h中的代碼如下:

#import @protocol DKSKeyboardDelegate @optional //非必實現的方法
/**
 點擊發送時輸入框內的文案
 @param textStr 文案
 */
- (void)textViewContentText:(NSString *)textStr;
/**
 鍵盤的frame改變
 */
- (void)keyboardChangeFrameWithMinY:(CGFloat)minY;
@end
@interface DKSKeyboardView : UIView @property (nonatomic, weak) id delegate;
@end

關于上面的兩個代理方法,由于文章篇幅問題,實現的過程可參考demo,里面有詳細的注釋;

在DKSKeyboardView.m中,以下列出少量重要代碼,主要是改變frame

1、點擊輸入框,鍵盤出現

//鍵盤將要出現
- (void)keyboardWillShow:(NSNotification *)notification {
  [self removeBottomViewFromSupview];
  NSDictionary *userInfo = notification.userInfo;
  CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  //獲取鍵盤的高度
  self.keyboardHeight = endFrame.size.height;
  
  //鍵盤的動畫時長
  CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  [UIView animateWithDuration:duration delay:0 options:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] animations:^{
    self.frame = CGRectMake(0, endFrame.origin.y - self.backView.height - StatusNav_Height, K_Width, self.height);
    [self changeTableViewFrame];
  } completion:nil];
}

2、鍵盤消失

- (void)keyboardWillHide:(NSNotification *)notification {
  //如果是彈出了底部視圖時
  if (self.moreClick || self.emojiClick) {
    return;
  }
  [UIView animateWithDuration:0.25 animations:^{
    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 = NO; //主要是設置表情按鈕為未點擊狀態
  if (self.moreClick == NO) {
    self.moreClick = YES;
    //回收鍵盤
    [self.textView resignFirstResponder];
    [self.emojiView removeFromSuperview];
    self.emojiView = nil;
    [self addSubview:self.moreView];
    //改變更多、self的frame
    [UIView animateWithDuration:0.25 animations:^{
      self.moreView.frame = CGRectMake(0, self.backView.height, 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 = height;
  self.textView.frame = frame; //改變輸入框的frame
  //當輸入框大小改變時,改變backView的frame
  self.backView.frame = CGRectMake(0, 0, K_Width, height + (viewMargin * 2));
  self.frame = CGRectMake(0, K_Height - self.backView.height - self.keyboardHeight, K_Width, self.backView.height);
  //改變更多按鈕、表情按鈕的位置
  self.emojiBtn.frame = CGRectMake(viewMargin, self.backView.height - viewHeight - viewMargin, viewHeight, viewHeight);
  self.moreBtn.frame = CGRectMake(self.textView.maxX + viewMargin, self.backView.height - viewHeight - viewMargin, viewHeight, viewHeight);
  //主要是為了改變VC的view的frame
  if (self.delegate && [self.delegate respondsToSelector:@selector(changeFrameWithMinY:)]) {
    [self.delegate changeFrameWithMinY:self.minY];
  }
}

關于怎么在iOS中實現聊天輸入框功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網站題目:怎么在iOS中實現聊天輸入框功能
地址分享:http://vcdvsql.cn/article18/jhihgp.html

成都網站建設公司_創新互聯,為您提供域名注冊網站排名網站建設網頁設計公司網站制作云服務器

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

商城網站建設