iOS開發之微信聊天工具欄的封裝

以前山寨了一個新浪微博(iOS開發之山寨版新浪微博小結),這幾天就山寨個微信吧。以前已經把微信的視圖結構簡單的拖了一下(iOS開發之微信山寨版),今天就開始給微信加上具體的實現功能,那麼就先從微信的聊天界面開始吧。提到封裝是少不了寫代碼的,在封裝組件的時候,爲了組件的可移植性,咱們就不能用storyboard來拖拽了。爲了屏幕的適配,適應不一樣屏幕的手機,因此在封裝組件的時候是少不了爲咱們的組件來添加約束。今天博客中的全部代碼都是脫離storyboard的,這些代碼在別的工程中也是可使用的。好,廢話少說,切入今天的正題。html

微信你們基本上都用過,今天要作的就是微信的聊天工具條。聊天工具條仍是比較複雜的,其中包括髮送表情,發送文字,發送圖片,發送聲音,拍照等等功能,下面給出發送錄音,文字,表情的代碼,其餘的和這幾樣相似。仍是那句話百字不如一圖,先來幾張效果圖吧。git

  

  

 

在封裝聊天工具條的的時候表情鍵盤是以前封裝好的(請參考:「iOS開發之自定義表情鍵盤(組件封裝與自動佈局)」),因此拿過來就能夠用的啦。由於不論是工具條仍是表情鍵盤都是用約束來控件大小的,因此橫屏也是沒問題的,在大屏手機上也是沒問題的。下面將會一步步講解如何封裝下面的聊天工具條。主要是對工具條的封裝,表情鍵盤在這就不作講解了。github

  

1、ToolView預留的接口sql

在封裝ToolView中主要用到Block回調,讀者能夠根據本身的我的習慣來選擇是Block回調,仍是委託回調或者是目標動做回調(筆者更喜歡Block回調),下面的代碼是ToolView給調用者提供的接口微信

 1 //
 2 //  ToolView.h
 3 //  MecroMessage
 4 //
 5 //  Created by (青玉伏案:博客地址(http://www.cnblogs.com/ludashi/)) on 14-9-22.
 6 //  Copyright (c) 2014年 Mrli. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 
12 //定義block類型把ToolView中TextView中的文字傳入到Controller中
13 typedef void (^MyTextBlock) (NSString *myText);
14 
15 //錄音時的音量
16 typedef void (^AudioVolumeBlock) (CGFloat volume);
17 
18 //錄音存儲地址
19 typedef void (^AudioURLBlock) (NSURL *audioURL);
20 
21 //改變根據文字改變TextView的高度
22 typedef void (^ContentSizeBlock)(CGSize contentSize);
23 
24 //錄音取消的回調
25 typedef void (^CancelRecordBlock)(int flag);
26 
27 
28 @interface ToolView : UIView<UITextViewDelegate,AVAudioRecorderDelegate>
29 
30 
31 //設置MyTextBlock
32 -(void) setMyTextBlock:(MyTextBlock)block;
33 
34 //設置聲音回調
35 -(void) setAudioVolumeBlock:(AudioVolumeBlock) block;
36 
37 //設置錄音地址回調
38 -(void) setAudioURLBlock:(AudioURLBlock) block;
39 
40 -(void)setContentSizeBlock:(ContentSizeBlock) block;
41 
42 -(void)setCancelRecordBlock:(CancelRecordBlock)block;
43 
44 -(void) changeFunctionHeight: (float) height;
45 
46 @end
View Code

  

2、初始化ToolView中所需的控件dom

1.爲了更好的封裝咱們的組件,在.h中預留接口,在ToolView.m的延展中添加咱們要使用的組件(私有屬性),延展代碼以下:ide

 1 @interface ToolView()
 2 //最左邊發送語音的按鈕
 3 @property (nonatomic, strong) UIButton *voiceChangeButton;
 4 
 5 //發送語音的按鈕
 6 @property (nonatomic, strong) UIButton *sendVoiceButton;
 7 
 8 //文本視圖
 9 @property (nonatomic, strong) UITextView *sendTextView;
10 
11 //切換鍵盤
12 @property (nonatomic, strong) UIButton *changeKeyBoardButton;
13 
14 //More
15 @property (nonatomic, strong) UIButton *moreButton;
16 
17 //鍵盤座標系的轉換
18 @property (nonatomic, assign) CGRect endKeyBoardFrame;
19 
20 
21 //表情鍵盤
22 @property (nonatomic, strong) FunctionView *functionView;
23 
24 //more
25 @property (nonatomic, strong) MoreView *moreView;
26 
27 //數據model
28 @property (strong, nonatomic) ImageModelClass  *imageMode;
29 
30 @property (strong, nonatomic)HistoryImage *tempImage;
31 
32 
33 //傳輸文字的block回調
34 @property (strong, nonatomic) MyTextBlock textBlock;
35 
36 //contentsinz
37 @property (strong, nonatomic) ContentSizeBlock sizeBlock;
38 
39 //傳輸volome的block回調
40 @property (strong, nonatomic) AudioVolumeBlock volumeBlock;
41 
42 //傳輸錄音地址
43 @property (strong, nonatomic) AudioURLBlock urlBlock;
44 
45 //錄音取消
46 @property (strong, nonatomic) CancelRecordBlock cancelBlock;
47 
48 
49 //添加錄音功能的屬性
50 @property (strong, nonatomic) AVAudioRecorder *audioRecorder;
51 
52 @property (strong, nonatomic) NSTimer *timer;
53 @property (strong, nonatomic) NSURL *audioPlayURL;
54 
55 @end
View Code

 

2.接受相應的Block回調,把block傳入ToolView中,代碼以下:  工具

 1 -(void)setMyTextBlock:(MyTextBlock)block
 2 {
 3     self.textBlock = block;
 4 }
 5 
 6 -(void)setAudioVolumeBlock:(AudioVolumeBlock)block
 7 {
 8     self.volumeBlock = block;
 9 }
10 
11 -(void)setAudioURLBlock:(AudioURLBlock)block
12 {
13     self.urlBlock = block;
14 }
15 
16 -(void)setContentSizeBlock:(ContentSizeBlock)block
17 {
18     self.sizeBlock = block;
19 }
20 
21 -(void)setCancelRecordBlock:(CancelRecordBlock)block
22 {
23     self.cancelBlock = block;
24 }
View Code

 

3.控件的初始化,純代碼添加ToolView中要用到的組件(分配內存,配置相應的屬性),由於是自定義組件的封裝,因此咱們的storyboard就用不上啦,添加控件的代碼以下:佈局

 1 //控件的初始化
 2 -(void) addSubview
 3 {
 4     self.voiceChangeButton = [[UIButton alloc] initWithFrame:CGRectZero];
 5     [self.voiceChangeButton setImage:[UIImage imageNamed:@"chat_bottom_voice_press.png"] forState:UIControlStateNormal];
 6     [self.voiceChangeButton addTarget:self action:@selector(tapVoiceChangeButton:) forControlEvents:UIControlEventTouchUpInside];
 7     [self addSubview:self.voiceChangeButton];
 8     
 9     self.sendVoiceButton = [[UIButton alloc] initWithFrame:CGRectZero];
10     [self.sendVoiceButton setBackgroundImage:[UIImage imageNamed:@"chat_bottom_textfield.png"] forState:UIControlStateNormal];
11     [self.sendVoiceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
12     [self.sendVoiceButton setTitle:@"按住說話" forState:UIControlStateNormal];
13     
14     
15     [self.sendVoiceButton addTarget:self action:@selector(tapSendVoiceButton:) forControlEvents:UIControlEventTouchUpInside];
16     self.sendVoiceButton.hidden = YES;
17     [self addSubview:self.sendVoiceButton];
18     
19     self.sendTextView = [[UITextView alloc] initWithFrame:CGRectZero];
20     self.sendTextView.delegate = self;
21     [self addSubview:self.sendTextView];
22     
23     self.changeKeyBoardButton = [[UIButton alloc] initWithFrame:CGRectZero];
24     [self.changeKeyBoardButton setImage:[UIImage imageNamed:@"chat_bottom_smile_nor.png"] forState:UIControlStateNormal];
25     [self.changeKeyBoardButton addTarget:self action:@selector(tapChangeKeyBoardButton:) forControlEvents:UIControlEventTouchUpInside];
26     [self addSubview:self.changeKeyBoardButton];
27     
28     self.moreButton = [[UIButton alloc] initWithFrame:CGRectZero];
29     [self.moreButton setImage:[UIImage imageNamed:@"chat_bottom_up_nor.png"] forState:UIControlStateNormal];
30     [self.moreButton addTarget:self action:@selector(tapMoreButton:) forControlEvents:UIControlEventTouchUpInside];
31     [self addSubview:self.moreButton];
32     
33     [self addDone];
34     
35     
36     
37     //實例化FunctionView
38     self.functionView = [[FunctionView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
39     self.functionView.backgroundColor = [UIColor blackColor];
40     
41     //設置資源加載的文件名
42     self.functionView.plistFileName = @"emoticons";
43     
44     __weak __block ToolView *copy_self = self;
45     //獲取圖片並顯示
46     [self.functionView setFunctionBlock:^(UIImage *image, NSString *imageText)
47      {
48          NSString *str = [NSString stringWithFormat:@"%@%@",copy_self.sendTextView.text, imageText];
49          
50          copy_self.sendTextView.text = str;
51          
52          //把使用過的圖片存入sqlite
53          NSData *imageData = UIImagePNGRepresentation(image);
54          [copy_self.imageMode save:imageData ImageText:imageText];
55      }];
56     
57     
58     //給sendTextView添加輕擊手勢
59     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
60     [self.sendTextView addGestureRecognizer:tapGesture];
61     
62     
63     //給sendVoiceButton添加長按手勢
64     UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(sendVoiceButtonLongPress:)];
65     //設置長按時間
66     longPress.minimumPressDuration = 0.2;
67     [self.sendVoiceButton addGestureRecognizer:longPress];
68     
69     //實例化MoreView
70     self.moreView = [[MoreView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
71     self.moreView.backgroundColor = [UIColor blackColor];
72     [self.moreView setMoreBlock:^(NSInteger index) {
73         NSLog(@"MoreIndex = %d",(int)index);
74     }];
75 
76     
77 }
View Code

 

4.給咱們的控件添加相應的約束,爲了適合不一樣的屏幕,因此自動佈局是少不了的。固然啦給控件添加約束也必須是手寫代碼啦,添加約束的代碼以下:post

 1 //給控件加約束
 2 -(void)addConstraint
 3 {
 4     //給voicebutton添加約束
 5     self.voiceChangeButton.translatesAutoresizingMaskIntoConstraints = NO;
 6     
 7     NSArray *voiceConstraintH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_voiceChangeButton(30)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_voiceChangeButton)];
 8     [self addConstraints:voiceConstraintH];
 9     
10     NSArray *voiceConstraintV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[_voiceChangeButton(30)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_voiceChangeButton)];
11     [self addConstraints:voiceConstraintV];
12     
13     
14     
15     //給MoreButton添加約束
16     self.moreButton.translatesAutoresizingMaskIntoConstraints = NO;
17     
18     NSArray *moreButtonH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[_moreButton(30)]-5-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_moreButton)];
19     [self addConstraints:moreButtonH];
20     
21     NSArray *moreButtonV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[_moreButton(30)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_moreButton)];
22     [self addConstraints:moreButtonV];
23     
24     
25     //給changeKeyBoardButton添加約束
26     self.changeKeyBoardButton.translatesAutoresizingMaskIntoConstraints = NO;
27     
28     NSArray *changeKeyBoardButtonH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[_changeKeyBoardButton(33)]-43-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_changeKeyBoardButton)];
29     [self addConstraints:changeKeyBoardButtonH];
30     
31     NSArray *changeKeyBoardButtonV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[_changeKeyBoardButton(33)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_changeKeyBoardButton)];
32     [self addConstraints:changeKeyBoardButtonV];
33     
34     
35     //給文本框添加約束
36     self.sendTextView.translatesAutoresizingMaskIntoConstraints = NO;
37     NSArray *sendTextViewConstraintH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-45-[_sendTextView]-80-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_sendTextView)];
38     [self addConstraints:sendTextViewConstraintH];
39     
40     NSArray *sendTextViewConstraintV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_sendTextView]-10-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_sendTextView)];
41     [self addConstraints:sendTextViewConstraintV];
42     
43     
44     //語音發送按鈕
45     self.sendVoiceButton.translatesAutoresizingMaskIntoConstraints = NO;
46     NSArray *sendVoiceButtonConstraintH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[_sendVoiceButton]-90-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_sendVoiceButton)];
47     [self addConstraints:sendVoiceButtonConstraintH];
48     
49     NSArray *sendVoiceButtonConstraintV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-6-[_sendVoiceButton]-6-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_sendVoiceButton)];
50     [self addConstraints:sendVoiceButtonConstraintV];
51     
52     
53 }
View Code

    

5.由於咱們要發送錄音,因此對音頻部分的初始化是少不了的,如下代碼是對音頻的初始化

 1 //錄音部分初始化
 2 -(void)audioInit
 3 {
 4     NSError * err = nil;
 5     
 6     AVAudioSession *audioSession = [AVAudioSession sharedInstance];
 7     [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
 8     
 9     if(err){
10         NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
11         return;
12     }
13     
14     [audioSession setActive:YES error:&err];
15     
16     err = nil;
17     if(err){
18         NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
19         return;
20     }
21 
22     //經過可變字典進行配置項的加載
23     NSMutableDictionary *setAudioDic = [[NSMutableDictionary alloc] init];
24     
25     //設置錄音格式(aac格式)
26     [setAudioDic setValue:@(kAudioFormatMPEG4AAC) forKey:AVFormatIDKey];
27     
28     //設置錄音採樣率(Hz) 如:AVSampleRateKey==8000/44100/96000(影響音頻的質量)
29     [setAudioDic setValue:@(44100) forKey:AVSampleRateKey];
30     
31     //設置錄音通道數1 Or 2
32     [setAudioDic setValue:@(1) forKey:AVNumberOfChannelsKey];
33     
34     //線性採樣位數  八、1六、2四、32
35     [setAudioDic setValue:@16 forKey:AVLinearPCMBitDepthKey];
36     //錄音的質量
37     [setAudioDic setValue:@(AVAudioQualityHigh) forKey:AVEncoderAudioQualityKey];
38     
39     NSString *strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
40     
41     NSString *fileName = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]];
42     
43     
44     NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.aac", strUrl, fileName]];
45     _audioPlayURL = url;
46     
47     NSError *error;
48     //初始化
49     self.audioRecorder = [[AVAudioRecorder alloc]initWithURL:url settings:setAudioDic error:&error];
50     //開啓音量檢測
51     self.audioRecorder.meteringEnabled = YES;
52     self.audioRecorder.delegate = self;
53 
54 }
View Code

    

6.添加鍵盤迴收鍵Done

 1 //給鍵盤添加done鍵
 2 -(void) addDone
 3 {
 4     //TextView的鍵盤定製回收按鈕
 5      UIToolbar * toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
 6  
 7    UIBarButtonItem * item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(tapDone:)];
 8     UIBarButtonItem * item2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
 9       UIBarButtonItem * item3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
10     toolBar.items = @[item2,item1,item3];
11     
12      self.sendTextView.inputAccessoryView =toolBar;
13 }
View Code

    

  

三.編寫控件的回調方法

控件添加好之後下面要添加觸發控件要乾的事情:

1.從最複雜的開始,長按發送錄音的按鈕時,會錄音。鬆開收時會發送(在發送時要判斷音頻的時間,過小不容許發送)。錄音時上滑取消錄音(刪除錄音文件)。主要是給錄音按鈕加了一個LongPress手勢,根據手勢的狀態來作不一樣的事情。關於手勢的內容請參考以前的博客:iOS開發之手勢識別,下面是錄音業務邏輯的實現(我的在Coding的時候,感受這一塊是工具條中最複雜的部分),代碼以下:  

 1 //長按手勢觸發的方法
 2 -(void)sendVoiceButtonLongPress:(id)sender
 3 {
 4     static int i = 1;
 5     if ([sender isKindOfClass:[UILongPressGestureRecognizer class]]) {
 6         
 7         UILongPressGestureRecognizer * longPress = sender;
 8         
 9         //錄音開始
10         if (longPress.state == UIGestureRecognizerStateBegan)
11         {
12             
13             i = 1;
14             
15             [self.sendVoiceButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
16             //錄音初始化
17             [self audioInit];
18             
19             //建立錄音文件,準備錄音
20             if ([self.audioRecorder prepareToRecord])
21             {
22                 //開始
23                 [self.audioRecorder record];
24                 
25                 //設置定時檢測音量變化
26                 _timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(detectionVoice) userInfo:nil repeats:YES];
27             }
28         }
29         
30         
31         //取消錄音
32         if (longPress.state == UIGestureRecognizerStateChanged)
33         {
34             
35             CGPoint piont = [longPress locationInView:self];
36             NSLog(@"%f",piont.y);
37 
38             if (piont.y < -20)
39             {
40                 if (i == 1) {
41                     
42                     [self.sendVoiceButton setBackgroundImage:[UIImage imageNamed:@"chat_bottom_textfield.png"] forState:UIControlStateNormal];
43                     [self.sendVoiceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
44                     //刪除錄製文件
45                     [self.audioRecorder deleteRecording];
46                     [self.audioRecorder stop];
47                     [_timer invalidate];
48                     
49                     UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"錄音取消" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
50                     [alter show];
51                     //去除圖片用的
52                     self.cancelBlock(1);
53                     i = 0;
54                     
55                 }
56 
57                 
58             }
59          }
60         
61         if (longPress.state == UIGestureRecognizerStateEnded) {
62             if (i == 1)
63             {
64                 NSLog(@"錄音結束");
65                 [self.sendVoiceButton setBackgroundImage:[UIImage imageNamed:@"chat_bottom_textfield.png"] forState:UIControlStateNormal];
66                 [self.sendVoiceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
67                 
68                 double cTime = self.audioRecorder.currentTime;
69                 if (cTime > 1)
70                 {
71                     //若是錄製時間<2 不發送
72                     NSLog(@"發出去");
73                     self.urlBlock(self.audioPlayURL);
74                 }
75                 else
76                 {
77                     //刪除記錄的文件
78                     [self.audioRecorder deleteRecording];
79                     UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"錄音時間過短!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
80                     [alter show];
81                     self.cancelBlock(1);
82                     
83                 }
84                 [self.audioRecorder stop];
85                 [_timer invalidate];
86             }
87         }
88         
89         
90     }
91     
92 }
View Code

    

2.下面的代碼是檢測音量的變化,用於根據音量變化圖片,代碼以下:

 1 //錄音的音量探測
 2 - (void)detectionVoice
 3 {
 4     [self.audioRecorder updateMeters];//刷新音量數據
 5     //獲取音量的平均值  [recorder averagePowerForChannel:0];
 6     //音量的最大值  [recorder peakPowerForChannel:0];
 7     
 8     CGFloat lowPassResults = pow(10, (0.05 * [self.audioRecorder peakPowerForChannel:0]));
 9     
10     //把聲音的音量傳給調用者
11     self.volumeBlock(lowPassResults);
12 }
View Code

    

3.輕擊輸入框時,切換到系統鍵盤,代碼以下:

 1 //輕擊sendText切換鍵盤
 2 -(void)tapGesture:(UITapGestureRecognizer *) sender
 3 {
 4     if ([self.sendTextView.inputView isEqual:self.functionView])
 5     {
 6         self.sendTextView.inputView = nil;
 7         
 8         [self.changeKeyBoardButton setImage:[UIImage imageNamed:@"chat_bottom_smile_nor.png"] forState:UIControlStateNormal];
 9         
10         [self.sendTextView reloadInputViews];
11     }
12     
13     if (![self.sendTextView isFirstResponder])
14     {
15         [self.sendTextView becomeFirstResponder];
16     }
17 }
View Code

    

4.經過輸入框的文字多少改變toolView的高度,由於輸入框的約束是加在ToolView上的,因此須要把輸入框的ContentSize經過block傳到ToolView的調用者上,讓ToolView的父視圖來改變ToolView的高度,從而sendTextView的高度也會隨着改變的,下面的代碼是把ContentSize交給父視圖:代碼以下:

1 //經過文字的多少改變toolView的高度
2 -(void)textViewDidChange:(UITextView *)textView
3 {
4     CGSize contentSize = self.sendTextView.contentSize;
5     
6     self.sizeBlock(contentSize);
7 }
View Code

    

效果以下,文字多時TextView的高度也會增大:

  

 

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

 1 //切換聲音按鍵和文字輸入框
 2 -(void)tapVoiceChangeButton:(UIButton *) sender
 3 {
 4 
 5     if (self.sendVoiceButton.hidden == YES)
 6     {
 7         self.sendTextView.hidden = YES;
 8         self.sendVoiceButton.hidden = NO;
 9         [self.voiceChangeButton setImage:[UIImage imageNamed:@"chat_bottom_keyboard_nor.png"] forState:UIControlStateNormal];
10         
11         if ([self.sendTextView isFirstResponder]) {
12             [self.sendTextView resignFirstResponder];
13         }
14     }
15     else
16     {
17         self.sendTextView.hidden = NO;
18         self.sendVoiceButton.hidden = YES;
19         [self.voiceChangeButton setImage:[UIImage imageNamed:@"chat_bottom_voice_press.png"] forState:UIControlStateNormal];
20         
21         if (![self.sendTextView isFirstResponder]) {
22             [self.sendTextView becomeFirstResponder];
23         }
24     }
25 }
View Code

    

6.點擊return發送文字(經過Block回調傳入到父視圖上),代碼以下:

 1 //發送信息(點擊return)
 2 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
 3 {
 4     if ([text isEqualToString:@"\n"])
 5     {
 6         
 7         //經過block回調把text的值傳遞到Controller中共
 8         self.textBlock(self.sendTextView.text);
 9         
10         self.sendTextView.text = @"";
11         
12         return NO;
13     }
14     return YES;
15 }
View Code

    

7.錄音按鈕自己要作的事情(在LongPress沒有被觸發時調用)代碼以下:

1 //發送聲音按鈕回調的方法
2 -(void)tapSendVoiceButton:(UIButton *) sender
3 {
4     NSLog(@"sendVoiceButton");
5     //點擊發送按鈕沒有觸發長按手勢要作的事兒
6     UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"按住錄音" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
7     [alter show];
8 }
View Code

    

8.調用表情鍵盤:

 1 //變成表情鍵盤
 2 -(void)tapChangeKeyBoardButton:(UIButton *) sender
 3 {
 4     if ([self.sendTextView.inputView isEqual:self.functionView])
 5     {
 6         self.sendTextView.inputView = nil;
 7         
 8         [self.changeKeyBoardButton setImage:[UIImage imageNamed:@"chat_bottom_smile_nor.png"] forState:UIControlStateNormal];
 9         
10         [self.sendTextView reloadInputViews];
11     }
12     else
13     {
14         self.sendTextView.inputView = self.functionView;
15        
16         
17         [self.changeKeyBoardButton setImage:[UIImage imageNamed:@"chat_bottom_keyboard_nor.png"] forState:UIControlStateNormal];
18         
19         [self.sendTextView reloadInputViews];
20     }
21     
22     if (![self.sendTextView isFirstResponder])
23     {
24         [self.sendTextView becomeFirstResponder];
25     }
26     
27     if (self.sendTextView.hidden == YES) {
28         self.sendTextView.hidden = NO;
29         self.sendVoiceButton.hidden = YES;
30         [self.voiceChangeButton setImage:[UIImage imageNamed:@"chat_bottom_voice_press.png"] forState:UIControlStateNormal];
31         
32     }
33 
34 }
View Code

 

以上就是ToolView的全部封裝代碼,至於在Controller中如何使用他來發送消息,如何定義聊天Cell,如何處理錄音文件,聊天時的氣泡是如何實現的等功能,在之後的博客中會繼續講解,但願你們繼續關注。(轉載請註明出處)

 Demo地址:https://github.com/lizelu/WeChat 

相關文章
相關標籤/搜索