之前山寨了一個(gè)新浪微博(iOS開(kāi)發(fā)之山寨版新浪微博小結(jié)),這幾天就山寨個(gè)微信吧。之前已經(jīng)把微信的視圖結(jié)構(gòu)簡(jiǎn)單的拖了一下(IOS開(kāi)發(fā)之微信山寨版),今天就開(kāi)始給微信加上具體的實(shí)現(xiàn)功能,那么就先從微信的聊天界面開(kāi)始吧。提到封裝是少不了寫(xiě)代碼的,在封裝組件的時(shí)候,為了組件的可移植性,我們就不能用storyboard來(lái)拖拽了。為了屏幕的適配,適應(yīng)不同屏幕的手機(jī),所以在封裝組件的時(shí)候是少不了為我們的組件來(lái)添加約束。今天博客中的所有代碼都是脫離storyboard的,這些代碼在別的工程中也是可以使用的。好,廢話少說(shuō),切入今天的正題。
微信大家基本上都用過(guò),今天要做的就是微信的聊天工具條。聊天工具條還是比較復(fù)雜的,其中包括發(fā)送表情,發(fā)送文字,發(fā)送圖片,發(fā)送聲音,拍照等等功能,下面給出發(fā)送錄音,文字,表情的代碼,其他的和這幾樣類似。還是那句話百字不如一圖,先來(lái)幾張效果圖吧。


在封裝聊天工具條的的時(shí)候表情鍵盤(pán)是之前封裝好的(請(qǐng)參考:“iOS開(kāi)發(fā)之自定義表情鍵盤(pán)(組件封裝與自動(dòng)布局)”),所以拿過(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)用者提供的接口

1 // 2 // ToolView.h 3 // MecroMessage 4 // 5 // Created by (青玉伏案:博客地址(http://m.survivalescaperooms.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 //錄音時(shí)的音量16 typedef void (^AudioVolumeBlock) (CGFloat volume);17 18 //錄音存儲(chǔ)地址19 typedef void (^AudioURLBlock) (NSURL *audioURL);20 21 //改變根據(jù)文字改變TextView的高度22 typedef void (^ContentSizeBlock)(CGSize contentSize);23 24 //錄音取消的回調(diào)25 typedef void (^CancelRecordBlock)(int flag);26 27 28 @interface ToolView : UIView<UITextViewDelegate,AVAudioRecorderDelegate>29 30 31 //設(shè)置MyTextBlock32 -(void) setMyTextBlock:(MyTextBlock)block;33 34 //設(shè)置聲音回調(diào)35 -(void) setAudioVolumeBlock:(AudioVolumeBlock) block;36 37 //設(shè)置錄音地址回調(diào)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
二:初始化ToolView中所需的控件
1.為了更好的封裝我們的組件,在.h中預(yù)留接口,在ToolView.m的延展中添加我們要使用的組件(私有屬性),延展代碼如下:

1 @interface ToolView() 2 //最左邊發(fā)送語(yǔ)音的按鈕 3 @PRoperty (nonatomic, strong) UIButton *voiceChangeButton; 4 5 //發(fā)送語(yǔ)音的按鈕 6 @property (nonatomic, strong) UIButton *sendVoiceButton; 7 8 //文本視圖 9 @property (nonatomic, strong) UITextView *sendTextView;10 11 //切換鍵盤(pán)12 @property (nonatomic, strong) UIButton *changeKeyBoardButton;13 14 //More15 @property (nonatomic, strong) UIButton *moreButton;16 17 //鍵盤(pán)坐標(biāo)系的轉(zhuǎn)換18 @property (nonatomic, assign) CGRect endKeyBoardFrame;19 20 21 //表情鍵盤(pán)22 @property (nonatomic, strong) FunctionView *functionView;23 24 //more25 @property (nonatomic, strong) MoreView *moreView;26 27 //數(shù)據(jù)model28 @property (strong, nonatomic) ImageModelClass *imageMode;29 30 @property (strong, nonatomic)HistoryImage *tempImage;31 32 33 //傳輸文字的block回調(diào)34 @property (strong, nonatomic) MyTextBlock textBlock;35 36 //contentsinz37 @property (strong, nonatomic) ContentSizeBlock sizeBlock;38 39 //傳輸volome的block回調(diào)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
2.接受相應(yīng)的Block回調(diào),把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)block12 {13 self.urlBlock = block;14 }15 16 -(void)setContentSizeBlock:(ContentSizeBlock)block17 {18 self.sizeBlock = block;19 }20 21 -(void)setCancelRecordBlock:(CancelRecordBlock)block22 {23 self.cancelBlock = block;24 }
3.控件的初始化,純代碼添加ToolView中要用到的組件(分配內(nèi)存,配置相應(yīng)的屬性),因?yàn)槭亲远x組件的封裝,所以我們的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:@"按住說(shuō)話" 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 //實(shí)例化FunctionView38 self.functionView = [[FunctionView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];39 self.functionView.backgroundColor = [UIColor blackColor];40 41 //設(shè)置資源加載的文件名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 //把使用過(guò)的圖片存入sqlite53 NSData *imageData = UIImagePNGRepresentation(image);54 [copy_self.imageMode save:imageData ImageText:imageText];55 }];56 57 58 //給sendTextView添加輕擊手勢(shì)59 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];60 [self.sendTextView addGestureRecognizer:tapGesture];61 62 63 //給sendVoiceButton添加長(zhǎng)按手勢(shì)64 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(sendVoiceButtonLongPress:)];65 //設(shè)置長(zhǎng)按時(shí)間66 longPress.minimumPressDuration = 0.2;67 [self.sendVoiceButton addGestureRecognizer:longPress];68 69 //實(shí)例化MoreView70 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 }
4.給我們的控件添加相應(yīng)的約束,為了適合不同的屏幕,所以自動(dòng)布局是少不了的。當(dāng)然啦給控件添加約束也必須是手寫(xiě)代碼啦,添加約束的代碼如下:

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 //語(yǔ)音發(fā)送按鈕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 }
5.因?yàn)槲覀円l(fā)送錄音,所以對(duì)音頻部分的初始化是少不了的,以下代碼是對(duì)音頻的初始化

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 //通過(guò)可變字典進(jìn)行配置項(xiàng)的加載23 NSMutableDictionary *setAudioDic = [[NSMutableDictionary alloc] init];24 25 //設(shè)置錄音格式(aac格式)26 [setAudioDic setValue:@(kAudioFormatMPEG4AAC) forKey:AVFormatIDKey];27 28 //設(shè)置錄音采樣率(Hz) 如:AVSampleRateKey==8000/44100/96000(影響音頻的質(zhì)量)29 [setAudioDic setValue:@(44100) forKey:AVSampleRateKey];30 31 //設(shè)置錄音通道數(shù)1 Or 232 [setAudioDic setValue:@(1) forKey:AVNumberOfChannelsKey];33 34 //線性采樣位數(shù) 8、16、24、3235 [setAudioDic setValue:@16 forKey:AVLinearPCMBitDepthKey];36 //錄音的質(zhì)量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 //開(kāi)啟音量檢測(cè)51 self.audioRecorder.meteringEnabled = YES;52 self.audioRecorder.delegate = self;53 54 }
6.添加鍵盤(pán)回收鍵Done

1 //給鍵盤(pán)添加done鍵 2 -(void) addDone 3 { 4 //TextView的鍵盤(pán)定制回收按鈕 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 }
三.編寫(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ù)雜的部分),代碼如下:

1 //長(zhǎng)按手勢(shì)觸發(fā)的方法 2 -(void)sendVoiceButtonLongPress:(id)sender 3 { 4 static int i = 1; 5 if ([sender isKindOfClass:[UILongPressGestureRecognizer class]]) { 6 7 UILongPressGestureRecognizer * longPress = sender; 8 9 //錄音開(kāi)始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 //創(chuàng)建錄音文件,準(zhǔn)備錄音20 if ([self.audioRecorder prepareToRecord])21 {22 //開(kāi)始23 [self.audioRecorder record];24 25 //設(shè)置定時(shí)檢測(cè)音量變化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(@"錄音結(jié)束");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 //如果錄制時(shí)間<2 不發(fā)送72 NSLog(@"發(fā)出去");73 self.urlBlock(self.audioPlayURL);74 }75 else76 {77 //刪除記錄的文件78 [self.audioRecorder deleteRecording];79 UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"錄音時(shí)間太短!" 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 }
2.下面的代碼是檢測(cè)音量的變化,用于根據(jù)音量變化圖片,代碼如下:

1 //錄音的音量探測(cè) 2 - (void)detectionVoice 3 { 4 [self.audioRecorder updateMeters];//刷新音量數(shù)據(jù) 5 //獲取音量的平均值 [recorder averagePowerForChannel:0]; 6 //音量的最大值 [recorder peakPowerForChannel:0]; 7 8 CGFloat lowPassResults = pow(10, (0.05 * [self.audioRecorder peakPowerForChannel:0])); 9 10 //把聲音的音量傳給調(diào)用者11 self.volumeBlock(lowPassResults);12 }
3.輕擊輸入框時(shí),切換到系統(tǒng)鍵盤(pán),代碼如下:

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

1 //通過(guò)文字的多少改變toolView的高度2 -(void)textViewDidChange:(UITextView *)textView3 {4 CGSize contentSize = self.sendTextView.contentSize;5 6 self.sizeBlock(contentSize);7 }
效果如下,文字多時(shí)TextView的高度也會(huì)增大:

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

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 else16 {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 }
6.點(diǎn)擊return發(fā)送文字(通過(guò)Block回調(diào)傳入到父視圖上),代碼如下:

1 //發(fā)送信息(點(diǎn)擊return) 2 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 3 { 4 if ([text isEqualToString:@"/n"]) 5 { 6 7 //通過(guò)block回調(diào)把text的值傳遞到Controller中共 8 self.textBlock(self.sendTextView.text); 9 10 self.sendTextView.text = @"";11 12 return NO;13 }14 return YES;15 }
7.錄音按鈕本身要做的事情(在LongPress沒(méi)有被觸發(fā)時(shí)調(diào)用)代碼如下:

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

1 //變成表情鍵盤(pán) 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 else13 {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 }
以上就是ToolView的所有封裝代碼,至于在Controller中如何使用他來(lái)發(fā)送消息,如何定義聊天Cell,如何處理錄音文件,聊天時(shí)的氣泡是如何實(shí)現(xiàn)的等功能,在以后的博客中會(huì)繼續(xù)講解,希望大家繼續(xù)關(guān)注。(轉(zhuǎn)載請(qǐng)注明出處)
Demo地址:https://github.com/lizelu/WeChat
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注