欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS開(kāi)發(fā)之微信聊天工具欄的封裝

 更新時(shí)間:2016年02月26日 17:20:44   作者:青玉伏案  
這篇文章主要為大家詳細(xì)介紹了iOS開(kāi)發(fā)之微信聊天工具欄的封裝,針對(duì)聊天工具條進(jìn)行封裝,感興趣的小伙伴們可以參考一下

微信大家基本上都用過(guò),今天要做的就是微信的聊天工具條。聊天工具條還是比較復(fù)雜的,其中包括發(fā)送表情,發(fā)送文字,發(fā)送圖片,發(fā)送聲音,拍照等等功能,下面給出發(fā)送錄音,文字,表情的代碼,其他的和這幾樣類似。還是那句話百字不如一圖,先來(lái)幾張效果圖吧。

在封裝聊天工具條的的時(shí)候表情鍵盤(pán)是之前封裝好的,所以拿過(guò)來(lái)就可以用的啦。因?yàn)椴还苁枪ぞ邨l還是表情鍵盤(pán)都是用約束來(lái)控件大小的,所以橫屏也是沒(méi)問(wèn)題的,在大屏手機(jī)上也是沒(méi)問(wèn)題的。下面將會(huì)一步步講解如何封裝下面的聊天工具條。主要是對(duì)工具條的封裝,表情鍵盤(pán)在這就不做講解了。
一、ToolView預(yù)留的接口
在封裝ToolView中主要用到Block回調(diào),讀者可以根據(jù)自己的個(gè)人習(xí)慣來(lái)選擇是Block回調(diào),還是委托回調(diào)或者是目標(biāo)動(dòng)作回調(diào)(筆者更喜歡Block回調(diào)),下面的代碼是ToolView給調(diào)用者提供的接口

//
// ToolView.h
// MecroMessage
//
// Created by (青玉伏案)on 14-9-22.
// Copyright (c) 2014年 Mrli. All rights reserved.
//

#import <UIKit/UIKit.h>


//定義block類型把ToolView中TextView中的文字傳入到Controller中
typedef void (^MyTextBlock) (NSString *myText);

//錄音時(shí)的音量
typedef void (^AudioVolumeBlock) (CGFloat volume);

//錄音存儲(chǔ)地址
typedef void (^AudioURLBlock) (NSURL *audioURL);

//改變根據(jù)文字改變TextView的高度
typedef void (^ContentSizeBlock)(CGSize contentSize);

//錄音取消的回調(diào)
typedef void (^CancelRecordBlock)(int flag);


@interface ToolView : UIView<UITextViewDelegate,AVAudioRecorderDelegate>


//設(shè)置MyTextBlock
-(void) setMyTextBlock:(MyTextBlock)block;

//設(shè)置聲音回調(diào)
-(void) setAudioVolumeBlock:(AudioVolumeBlock) block;

//設(shè)置錄音地址回調(diào)
-(void) setAudioURLBlock:(AudioURLBlock) block;

-(void)setContentSizeBlock:(ContentSizeBlock) block;

-(void)setCancelRecordBlock:(CancelRecordBlock)block;

-(void) changeFunctionHeight: (float) height;

@end


二、初始化ToolView中所需的控件
1.為了更好的封裝我們的組件,在.h中預(yù)留接口,在ToolView.m的延展中添加我們要使用的組件(私有屬性),延展代碼如下:

@interface ToolView()
//最左邊發(fā)送語(yǔ)音的按鈕
@property (nonatomic, strong) UIButton *voiceChangeButton;

//發(fā)送語(yǔ)音的按鈕
@property (nonatomic, strong) UIButton *sendVoiceButton;

//文本視圖
@property (nonatomic, strong) UITextView *sendTextView;

//切換鍵盤(pán)
@property (nonatomic, strong) UIButton *changeKeyBoardButton;

//More
@property (nonatomic, strong) UIButton *moreButton;

//鍵盤(pán)坐標(biāo)系的轉(zhuǎn)換
@property (nonatomic, assign) CGRect endKeyBoardFrame;


//表情鍵盤(pán)
@property (nonatomic, strong) FunctionView *functionView;

//more
@property (nonatomic, strong) MoreView *moreView;

//數(shù)據(jù)model
@property (strong, nonatomic) ImageModelClass *imageMode;

@property (strong, nonatomic)HistoryImage *tempImage;


//傳輸文字的block回調(diào)
@property (strong, nonatomic) MyTextBlock textBlock;

//contentsinz
@property (strong, nonatomic) ContentSizeBlock sizeBlock;

//傳輸volome的block回調(diào)
@property (strong, nonatomic) AudioVolumeBlock volumeBlock;

//傳輸錄音地址
@property (strong, nonatomic) AudioURLBlock urlBlock;

//錄音取消
@property (strong, nonatomic) CancelRecordBlock cancelBlock;


//添加錄音功能的屬性
@property (strong, nonatomic) AVAudioRecorder *audioRecorder;

@property (strong, nonatomic) NSTimer *timer;
@property (strong, nonatomic) NSURL *audioPlayURL;

@end

2.接受相應(yīng)的Block回調(diào),把block傳入ToolView中,代碼如下:  

-(void)setMyTextBlock:(MyTextBlock)block
{
 self.textBlock = block;
}

-(void)setAudioVolumeBlock:(AudioVolumeBlock)block
{
 self.volumeBlock = block;
}

-(void)setAudioURLBlock:(AudioURLBlock)block
{
 self.urlBlock = block;
}

-(void)setContentSizeBlock:(ContentSizeBlock)block
{
 self.sizeBlock = block;
}

-(void)setCancelRecordBlock:(CancelRecordBlock)block
{
 self.cancelBlock = block;
}

3.控件的初始化,純代碼添加ToolView中要用到的組件(分配內(nèi)存,配置相應(yīng)的屬性),因?yàn)槭亲远x組件的封裝,所以我們的storyboard就用不上啦,添加控件的代碼如下:

//控件的初始化
-(void) addSubview
{
 self.voiceChangeButton = [[UIButton alloc] initWithFrame:CGRectZero];
 [self.voiceChangeButton setImage:[UIImage imageNamed:@"chat_bottom_voice_press.png"] forState:UIControlStateNormal];
 [self.voiceChangeButton addTarget:self action:@selector(tapVoiceChangeButton:) forControlEvents:UIControlEventTouchUpInside];
 [self addSubview:self.voiceChangeButton];
 
 self.sendVoiceButton = [[UIButton alloc] initWithFrame:CGRectZero];
 [self.sendVoiceButton setBackgroundImage:[UIImage imageNamed:@"chat_bottom_textfield.png"] forState:UIControlStateNormal];
 [self.sendVoiceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 [self.sendVoiceButton setTitle:@"按住說(shuō)話" forState:UIControlStateNormal];
 
 
 [self.sendVoiceButton addTarget:self action:@selector(tapSendVoiceButton:) forControlEvents:UIControlEventTouchUpInside];
 self.sendVoiceButton.hidden = YES;
 [self addSubview:self.sendVoiceButton];
 
 self.sendTextView = [[UITextView alloc] initWithFrame:CGRectZero];
 self.sendTextView.delegate = self;
 [self addSubview:self.sendTextView];
 
 self.changeKeyBoardButton = [[UIButton alloc] initWithFrame:CGRectZero];
 [self.changeKeyBoardButton setImage:[UIImage imageNamed:@"chat_bottom_smile_nor.png"] forState:UIControlStateNormal];
 [self.changeKeyBoardButton addTarget:self action:@selector(tapChangeKeyBoardButton:) forControlEvents:UIControlEventTouchUpInside];
 [self addSubview:self.changeKeyBoardButton];
 
 self.moreButton = [[UIButton alloc] initWithFrame:CGRectZero];
 [self.moreButton setImage:[UIImage imageNamed:@"chat_bottom_up_nor.png"] forState:UIControlStateNormal];
 [self.moreButton addTarget:self action:@selector(tapMoreButton:) forControlEvents:UIControlEventTouchUpInside];
 [self addSubview:self.moreButton];
 
 [self addDone];
 
 
 
 //實(shí)例化FunctionView
 self.functionView = [[FunctionView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
 self.functionView.backgroundColor = [UIColor blackColor];
 
 //設(shè)置資源加載的文件名
 self.functionView.plistFileName = @"emoticons";
 
 __weak __block ToolView *copy_self = self;
 //獲取圖片并顯示
 [self.functionView setFunctionBlock:^(UIImage *image, NSString *imageText)
  {
   NSString *str = [NSString stringWithFormat:@"%@%@",copy_self.sendTextView.text, imageText];
   
   copy_self.sendTextView.text = str;
   
   //把使用過(guò)的圖片存入sqlite
   NSData *imageData = UIImagePNGRepresentation(image);
   [copy_self.imageMode save:imageData ImageText:imageText];
  }];
 
 
 //給sendTextView添加輕擊手勢(shì)
 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
 [self.sendTextView addGestureRecognizer:tapGesture];
 
 
 //給sendVoiceButton添加長(zhǎng)按手勢(shì)
 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(sendVoiceButtonLongPress:)];
 //設(shè)置長(zhǎng)按時(shí)間
 longPress.minimumPressDuration = 0.2;
 [self.sendVoiceButton addGestureRecognizer:longPress];
 
 //實(shí)例化MoreView
 self.moreView = [[MoreView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
 self.moreView.backgroundColor = [UIColor blackColor];
 [self.moreView setMoreBlock:^(NSInteger index) {
  NSLog(@"MoreIndex = %d",(int)index);
 }];

 
}

4.給我們的控件添加相應(yīng)的約束,為了適合不同的屏幕,所以自動(dòng)布局是少不了的。當(dāng)然啦給控件添加約束也必須是手寫(xiě)代碼啦,添加約束的代碼如下:

//給控件加約束
-(void)addConstraint
{
 //給voicebutton添加約束
 self.voiceChangeButton.translatesAutoresizingMaskIntoConstraints = NO;
 
 NSArray *voiceConstraintH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_voiceChangeButton(30)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_voiceChangeButton)];
 [self addConstraints:voiceConstraintH];
 
 NSArray *voiceConstraintV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[_voiceChangeButton(30)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_voiceChangeButton)];
 [self addConstraints:voiceConstraintV];
 
 
 
 //給MoreButton添加約束
 self.moreButton.translatesAutoresizingMaskIntoConstraints = NO;
 
 NSArray *moreButtonH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[_moreButton(30)]-5-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_moreButton)];
 [self addConstraints:moreButtonH];
 
 NSArray *moreButtonV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[_moreButton(30)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_moreButton)];
 [self addConstraints:moreButtonV];
 
 
 //給changeKeyBoardButton添加約束
 self.changeKeyBoardButton.translatesAutoresizingMaskIntoConstraints = NO;
 
 NSArray *changeKeyBoardButtonH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[_changeKeyBoardButton(33)]-43-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_changeKeyBoardButton)];
 [self addConstraints:changeKeyBoardButtonH];
 
 NSArray *changeKeyBoardButtonV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[_changeKeyBoardButton(33)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_changeKeyBoardButton)];
 [self addConstraints:changeKeyBoardButtonV];
 
 
 //給文本框添加約束
 self.sendTextView.translatesAutoresizingMaskIntoConstraints = NO;
 NSArray *sendTextViewConstraintH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-45-[_sendTextView]-80-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_sendTextView)];
 [self addConstraints:sendTextViewConstraintH];
 
 NSArray *sendTextViewConstraintV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_sendTextView]-10-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_sendTextView)];
 [self addConstraints:sendTextViewConstraintV];
 
 
 //語(yǔ)音發(fā)送按鈕
 self.sendVoiceButton.translatesAutoresizingMaskIntoConstraints = NO;
 NSArray *sendVoiceButtonConstraintH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[_sendVoiceButton]-90-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_sendVoiceButton)];
 [self addConstraints:sendVoiceButtonConstraintH];
 
 NSArray *sendVoiceButtonConstraintV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-6-[_sendVoiceButton]-6-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_sendVoiceButton)];
 [self addConstraints:sendVoiceButtonConstraintV];
 
 
}

5.因?yàn)槲覀円l(fā)送錄音,所以對(duì)音頻部分的初始化是少不了的,以下代碼是對(duì)音頻的初始化

//錄音部分初始化
-(void)audioInit
{
 NSError * err = nil;
 
 AVAudioSession *audioSession = [AVAudioSession sharedInstance];
 [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
 
 if(err){
  NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
  return;
 }
 
 [audioSession setActive:YES error:&err];
 
 err = nil;
 if(err){
  NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
  return;
 }

 //通過(guò)可變字典進(jìn)行配置項(xiàng)的加載
 NSMutableDictionary *setAudioDic = [[NSMutableDictionary alloc] init];
 
 //設(shè)置錄音格式(aac格式)
 [setAudioDic setValue:@(kAudioFormatMPEG4AAC) forKey:AVFormatIDKey];
 
 //設(shè)置錄音采樣率(Hz) 如:AVSampleRateKey==8000/44100/96000(影響音頻的質(zhì)量)
 [setAudioDic setValue:@(44100) forKey:AVSampleRateKey];
 
 //設(shè)置錄音通道數(shù)1 Or 2
 [setAudioDic setValue:@(1) forKey:AVNumberOfChannelsKey];
 
 //線性采樣位數(shù) 8、16、24、32
 [setAudioDic setValue:@16 forKey:AVLinearPCMBitDepthKey];
 //錄音的質(zhì)量
 [setAudioDic setValue:@(AVAudioQualityHigh) forKey:AVEncoderAudioQualityKey];
 
 NSString *strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
 
 NSString *fileName = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]];
 
 
 NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.aac", strUrl, fileName]];
 _audioPlayURL = url;
 
 NSError *error;
 //初始化
 self.audioRecorder = [[AVAudioRecorder alloc]initWithURL:url settings:setAudioDic error:&error];
 //開(kāi)啟音量檢測(cè)
 self.audioRecorder.meteringEnabled = YES;
 self.audioRecorder.delegate = self;

}

6.添加鍵盤(pán)回收鍵Done

//給鍵盤(pán)添加done鍵
-(void) addDone
{
 //TextView的鍵盤(pán)定制回收按鈕
  UIToolbar * toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
 
 UIBarButtonItem * item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(tapDone:)];
 UIBarButtonItem * item2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  UIBarButtonItem * item3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
 toolBar.items = @[item2,item1,item3];
 
  self.sendTextView.inputAccessoryView =toolBar;
}

三.編寫(xiě)控件的回調(diào)方法
控件添加好以后下面要添加觸發(fā)控件要干的事情:
1.從最復(fù)雜的開(kāi)始,長(zhǎng)按發(fā)送錄音的按鈕時(shí),會(huì)錄音。松開(kāi)收時(shí)會(huì)發(fā)送(在發(fā)送時(shí)要判斷音頻的時(shí)間,太小不允許發(fā)送)。錄音時(shí)上滑取消錄音(刪除錄音文件)。主要是給錄音按鈕加了一個(gè)LongPress手勢(shì),根據(jù)手勢(shì)的狀態(tài)來(lái)做不同的事情。關(guān)于手勢(shì)的內(nèi)容請(qǐng)參考之前的博客:(iOS開(kāi)發(fā)之手勢(shì)識(shí)別),下面是錄音業(yè)務(wù)邏輯的實(shí)現(xiàn)(個(gè)人在Coding的時(shí)候,感覺(jué)這一塊是工具條中最復(fù)雜的部分),代碼如下:  

//長(zhǎng)按手勢(shì)觸發(fā)的方法
-(void)sendVoiceButtonLongPress:(id)sender
{
 static int i = 1;
 if ([sender isKindOfClass:[UILongPressGestureRecognizer class]]) {
  
  UILongPressGestureRecognizer * longPress = sender;
  
  //錄音開(kāi)始
  if (longPress.state == UIGestureRecognizerStateBegan)
  {
   
   i = 1;
   
   [self.sendVoiceButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
   //錄音初始化
   [self audioInit];
   
   //創(chuàng)建錄音文件,準(zhǔn)備錄音
   if ([self.audioRecorder prepareToRecord])
   {
    //開(kāi)始
    [self.audioRecorder record];
    
    //設(shè)置定時(shí)檢測(cè)音量變化
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(detectionVoice) userInfo:nil repeats:YES];
   }
  }
  
  
  //取消錄音
  if (longPress.state == UIGestureRecognizerStateChanged)
  {
   
   CGPoint piont = [longPress locationInView:self];
   NSLog(@"%f",piont.y);

   if (piont.y < -20)
   {
    if (i == 1) {
     
     [self.sendVoiceButton setBackgroundImage:[UIImage imageNamed:@"chat_bottom_textfield.png"] forState:UIControlStateNormal];
     [self.sendVoiceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
     //刪除錄制文件
     [self.audioRecorder deleteRecording];
     [self.audioRecorder stop];
     [_timer invalidate];
     
     UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"錄音取消" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
     [alter show];
     //去除圖片用的
     self.cancelBlock(1);
     i = 0;
     
    }

    
   }
   }
  
  if (longPress.state == UIGestureRecognizerStateEnded) {
   if (i == 1)
   {
    NSLog(@"錄音結(jié)束");
    [self.sendVoiceButton setBackgroundImage:[UIImage imageNamed:@"chat_bottom_textfield.png"] forState:UIControlStateNormal];
    [self.sendVoiceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    double cTime = self.audioRecorder.currentTime;
    if (cTime > 1)
    {
     //如果錄制時(shí)間<2 不發(fā)送
     NSLog(@"發(fā)出去");
     self.urlBlock(self.audioPlayURL);
    }
    else
    {
     //刪除記錄的文件
     [self.audioRecorder deleteRecording];
     UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"錄音時(shí)間太短!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
     [alter show];
     self.cancelBlock(1);
     
    }
    [self.audioRecorder stop];
    [_timer invalidate];
   }
  }
  
  
 }
 
}

2.下面的代碼是檢測(cè)音量的變化,用于根據(jù)音量變化圖片,代碼如下:

//錄音的音量探測(cè)
- (void)detectionVoice
{
 [self.audioRecorder updateMeters];//刷新音量數(shù)據(jù)
 //獲取音量的平均值 [recorder averagePowerForChannel:0];
 //音量的最大值 [recorder peakPowerForChannel:0];
 
 CGFloat lowPassResults = pow(10, (0.05 * [self.audioRecorder peakPowerForChannel:0]));
 
 //把聲音的音量傳給調(diào)用者
 self.volumeBlock(lowPassResults);
}

3.輕擊輸入框時(shí),切換到系統(tǒng)鍵盤(pán),代碼如下:

//輕擊sendText切換鍵盤(pán)
-(void)tapGesture:(UITapGestureRecognizer *) sender
{
 if ([self.sendTextView.inputView isEqual:self.functionView])
 {
  self.sendTextView.inputView = nil;
  
  [self.changeKeyBoardButton setImage:[UIImage imageNamed:@"chat_bottom_smile_nor.png"] forState:UIControlStateNormal];
  
  [self.sendTextView reloadInputViews];
 }
 
 if (![self.sendTextView isFirstResponder])
 {
  [self.sendTextView becomeFirstResponder];
 }
}

4.通過(guò)輸入框的文字多少改變toolView的高度,因?yàn)檩斎肟虻募s束是加在ToolView上的,所以需要把輸入框的ContentSize通過(guò)block傳到ToolView的調(diào)用者上,讓ToolView的父視圖來(lái)改變ToolView的高度,從而sendTextView的高度也會(huì)隨著改變的,下面的代碼是把ContentSize交給父視圖:代碼如下:

//通過(guò)文字的多少改變toolView的高度
-(void)textViewDidChange:(UITextView *)textView
{
 CGSize contentSize = self.sendTextView.contentSize;
 
 self.sizeBlock(contentSize);
}

效果如下,文字多時(shí)TextView的高度也會(huì)增大:

5.點(diǎn)擊最左邊的按鈕觸發(fā)的事件(切換文本輸入框和錄音按鈕),代碼如下:

//切換聲音按鍵和文字輸入框
-(void)tapVoiceChangeButton:(UIButton *) sender
{

 if (self.sendVoiceButton.hidden == YES)
 {
  self.sendTextView.hidden = YES;
  self.sendVoiceButton.hidden = NO;
  [self.voiceChangeButton setImage:[UIImage imageNamed:@"chat_bottom_keyboard_nor.png"] forState:UIControlStateNormal];
  
  if ([self.sendTextView isFirstResponder]) {
   [self.sendTextView resignFirstResponder];
  }
 }
 else
 {
  self.sendTextView.hidden = NO;
  self.sendVoiceButton.hidden = YES;
  [self.voiceChangeButton setImage:[UIImage imageNamed:@"chat_bottom_voice_press.png"] forState:UIControlStateNormal];
  
  if (![self.sendTextView isFirstResponder]) {
   [self.sendTextView becomeFirstResponder];
  }
 }
}

6.點(diǎn)擊return發(fā)送文字(通過(guò)Block回調(diào)傳入到父視圖上),代碼如下:

//發(fā)送信息(點(diǎn)擊return)
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
 if ([text isEqualToString:@"\n"])
 {
  
  //通過(guò)block回調(diào)把text的值傳遞到Controller中共
  self.textBlock(self.sendTextView.text);
  
  self.sendTextView.text = @"";
  
  return NO;
 }
 return YES;
}

7.錄音按鈕本身要做的事情(在LongPress沒(méi)有被觸發(fā)時(shí)調(diào)用)代碼如下:

//發(fā)送聲音按鈕回調(diào)的方法
-(void)tapSendVoiceButton:(UIButton *) sender
{
 NSLog(@"sendVoiceButton");
 //點(diǎn)擊發(fā)送按鈕沒(méi)有觸發(fā)長(zhǎng)按手勢(shì)要做的事兒
 UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"按住錄音" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
 [alter show];
}

8.調(diào)用表情鍵盤(pán):

//變成表情鍵盤(pán)
-(void)tapChangeKeyBoardButton:(UIButton *) sender
{
 if ([self.sendTextView.inputView isEqual:self.functionView])
 {
  self.sendTextView.inputView = nil;
  
  [self.changeKeyBoardButton setImage:[UIImage imageNamed:@"chat_bottom_smile_nor.png"] forState:UIControlStateNormal];
  
  [self.sendTextView reloadInputViews];
 }
 else
 {
  self.sendTextView.inputView = self.functionView;
  
  
  [self.changeKeyBoardButton setImage:[UIImage imageNamed:@"chat_bottom_keyboard_nor.png"] forState:UIControlStateNormal];
  
  [self.sendTextView reloadInputViews];
 }
 
 if (![self.sendTextView isFirstResponder])
 {
  [self.sendTextView becomeFirstResponder];
 }
 
 if (self.sendTextView.hidden == YES) {
  self.sendTextView.hidden = NO;
  self.sendVoiceButton.hidden = YES;
  [self.voiceChangeButton setImage:[UIImage imageNamed:@"chat_bottom_voice_press.png"] forState:UIControlStateNormal];
  
 }

}

以上就是ToolView的所有封裝代碼,至于在Controller中如何使用他來(lái)發(fā)送消息,如何定義聊天Cell,如何處理錄音文件,聊天時(shí)的氣泡是如何實(shí)現(xiàn)的等功能,在以后的文章中會(huì)繼續(xù)講解,希望大家繼續(xù)關(guān)注。

相關(guān)文章

最新評(píng)論