Skip to content
Navigation Menu
{{ message }}
forked from ZhipingYang/UUChatTableView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUUInputFunctionView.m
More file actions
276 lines (236 loc) · 10.4 KB
/
Copy pathUUInputFunctionView.m
File metadata and controls
276 lines (236 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// UUInputFunctionView.m
// UUChatDemoForTextVoicePicture
//
// Created by shake on 14-8-27.
// Copyright (c) 2014年 uyiuyao. All rights reserved.
//
#import "UUInputFunctionView.h"
#import "Mp3Recorder.h"
#import "UUProgressHUD.h"
@interface UUInputFunctionView ()<UITextViewDelegate,Mp3RecorderDelegate>
{
BOOL isbeginVoiceRecord;
Mp3Recorder *MP3;
NSInteger playTime;
NSTimer *playTimer;
UILabel *placeHold;
}
@end
@implementation UUInputFunctionView
- (id)initWithSuperVC:(UIViewController *)superVC
{
self.superVC = superVC;
CGRect frame = CGRectMake(0, Main_Screen_Height-40, Main_Screen_Width, 40);
self = [super initWithFrame:frame];
if (self) {
MP3 = [[Mp3Recorder alloc]initWithDelegate:self];
self.backgroundColor = [UIColor whiteColor];
//发送消息
self.btnSendMessage = [UIButton buttonWithType:UIButtonTypeCustom];
self.btnSendMessage.frame = CGRectMake(Main_Screen_Width-40, 5, 30, 30);
self.isAbleToSendTextMessage = NO;
[self.btnSendMessage setTitle:@"" forState:UIControlStateNormal];
[self.btnSendMessage setBackgroundImage:[UIImage imageNamed:@"Chat_take_picture"] forState:UIControlStateNormal];
self.btnSendMessage.titleLabel.font = [UIFont systemFontOfSize:12];
[self.btnSendMessage addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.btnSendMessage];
//改变状态(语音、文字)
self.btnChangeVoiceState = [UIButton buttonWithType:UIButtonTypeCustom];
self.btnChangeVoiceState.frame = CGRectMake(5, 5, 30, 30);
isbeginVoiceRecord = NO;
[self.btnChangeVoiceState setBackgroundImage:[UIImage imageNamed:@"chat_voice_record"] forState:UIControlStateNormal];
self.btnChangeVoiceState.titleLabel.font = [UIFont systemFontOfSize:12];
[self.btnChangeVoiceState addTarget:self action:@selector(voiceRecord:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.btnChangeVoiceState];
//语音录入键
self.btnVoiceRecord = [UIButton buttonWithType:UIButtonTypeCustom];
self.btnVoiceRecord.frame = CGRectMake(70, 5, Main_Screen_Width-70*2, 30);
self.btnVoiceRecord.hidden = YES;
[self.btnVoiceRecord setBackgroundImage:[UIImage imageNamed:@"chat_message_back"] forState:UIControlStateNormal];
[self.btnVoiceRecord setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[self.btnVoiceRecord setTitleColor:[[UIColor lightGrayColor] colorWithAlphaComponent:0.5] forState:UIControlStateHighlighted];
[self.btnVoiceRecord setTitle:@"Hold to Talk" forState:UIControlStateNormal];
[self.btnVoiceRecord setTitle:@"Release to Send" forState:UIControlStateHighlighted];
[self.btnVoiceRecord addTarget:self action:@selector(beginRecordVoice:) forControlEvents:UIControlEventTouchDown];
[self.btnVoiceRecord addTarget:self action:@selector(endRecordVoice:) forControlEvents:UIControlEventTouchUpInside];
[self.btnVoiceRecord addTarget:self action:@selector(cancelRecordVoice:) forControlEvents:UIControlEventTouchUpOutside | UIControlEventTouchCancel];
[self.btnVoiceRecord addTarget:self action:@selector(RemindDragExit:) forControlEvents:UIControlEventTouchDragExit];
[self.btnVoiceRecord addTarget:self action:@selector(RemindDragEnter:) forControlEvents:UIControlEventTouchDragEnter];
[self addSubview:self.btnVoiceRecord];
//输入框
self.TextViewInput = [[UITextView alloc]initWithFrame:CGRectMake(45, 5, Main_Screen_Width-2*45, 30)];
self.TextViewInput.layer.cornerRadius = 4;
self.TextViewInput.layer.masksToBounds = YES;
self.TextViewInput.delegate = self;
self.TextViewInput.layer.borderWidth = 1;
self.TextViewInput.layer.borderColor = [[[UIColor lightGrayColor] colorWithAlphaComponent:0.4] CGColor];
[self addSubview:self.TextViewInput];
//输入框的提示语
placeHold = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 200, 30)];
placeHold.text = @"Input the contents here";
placeHold.textColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.8];
[self.TextViewInput addSubview:placeHold];
//分割线
self.layer.borderWidth = 1;
self.layer.borderColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3].CGColor;
//添加通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewDidEndEditing:) name:UIKeyboardWillHideNotification object:nil];
}
return self;
}
#pragma mark - 录音touch事件
- (void)beginRecordVoice:(UIButton *)button
{
[MP3 startRecord];
playTime = 0;
playTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countVoiceTime) userInfo:nil repeats:YES];
[UUProgressHUD show];
}
- (void)endRecordVoice:(UIButton *)button
{
if (playTimer) {
[MP3 stopRecord];
[playTimer invalidate];
playTimer = nil;
}
}
- (void)cancelRecordVoice:(UIButton *)button
{
if (playTimer) {
[MP3 cancelRecord];
[playTimer invalidate];
playTimer = nil;
}
[UUProgressHUD dismissWithError:@"Cancel"];
}
- (void)RemindDragExit:(UIButton *)button
{
[UUProgressHUD changeSubTitle:@"Release to cancel"];
}
- (void)RemindDragEnter:(UIButton *)button
{
[UUProgressHUD changeSubTitle:@"Slide up to cancel"];
}
- (void)countVoiceTime
{
playTime ++;
if (playTime>=60) {
[self endRecordVoice:nil];
}
}
#pragma mark - Mp3RecorderDelegate
//回调录音资料
- (void)endConvertWithData:(NSData *)voiceData
{
[self.delegate UUInputFunctionView:self sendVoice:voiceData time:playTime+1];
[UUProgressHUD dismissWithSuccess:@"Success"];
//缓冲消失时间 (最好有block回调消失完成)
self.btnVoiceRecord.enabled = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.btnVoiceRecord.enabled = YES;
});
}
- (void)failRecord
{
[UUProgressHUD dismissWithSuccess:@"Too short"];
//缓冲消失时间 (最好有block回调消失完成)
self.btnVoiceRecord.enabled = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.btnVoiceRecord.enabled = YES;
});
}
//改变输入与录音状态
- (void)voiceRecord:(UIButton *)sender
{
self.btnVoiceRecord.hidden = !self.btnVoiceRecord.hidden;
self.TextViewInput.hidden = !self.TextViewInput.hidden;
isbeginVoiceRecord = !isbeginVoiceRecord;
if (isbeginVoiceRecord) {
[self.btnChangeVoiceState setBackgroundImage:[UIImage imageNamed:@"chat_ipunt_message"] forState:UIControlStateNormal];
[self.TextViewInput resignFirstResponder];
}else{
[self.btnChangeVoiceState setBackgroundImage:[UIImage imageNamed:@"chat_voice_record"] forState:UIControlStateNormal];
[self.TextViewInput becomeFirstResponder];
}
}
//发送消息(文字图片)
- (void)sendMessage:(UIButton *)sender
{
if (self.isAbleToSendTextMessage) {
NSString *resultStr = [self.TextViewInput.text stringByReplacingOccurrencesOfString:@" " withString:@""];
[self.delegate UUInputFunctionView:self sendMessage:resultStr];
}
else{
[self.TextViewInput resignFirstResponder];
UIActionSheet *actionSheet= [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera",@"Images",nil];
[actionSheet showInView:self.window];
}
}
#pragma mark - TextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
placeHold.hidden = self.TextViewInput.text.length > 0;
}
- (void)textViewDidChange:(UITextView *)textView
{
[self changeSendBtnWithPhoto:textView.text.length>0?NO:YES];
placeHold.hidden = textView.text.length>0;
}
- (void)changeSendBtnWithPhoto:(BOOL)isPhoto
{
self.isAbleToSendTextMessage = !isPhoto;
[self.btnSendMessage setTitle:isPhoto?@"":@"send" forState:UIControlStateNormal];
self.btnSendMessage.frame = RECT_CHANGE_width(self.btnSendMessage, isPhoto?30:35);
UIImage *image = [UIImage imageNamed:isPhoto?@"Chat_take_picture":@"chat_send_message"];
[self.btnSendMessage setBackgroundImage:image forState:UIControlStateNormal];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
placeHold.hidden = self.TextViewInput.text.length > 0;
}
#pragma mark - Add Picture
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
[self addCarema];
}else if (buttonIndex == 1){
[self openPicLibrary];
}
}
-(void)addCarema{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self.superVC presentViewController:picker animated:YES completion:^{}];
}else{
//如果没有提示用户
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Tip" message:@"Your device don't have camera" delegate:nil cancelButtonTitle:@"Sure" otherButtonTitles:nil];
[alert show];
}
}
-(void)openPicLibrary{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self.superVC presentViewController:picker animated:YES completion:^{
}];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *editImage = [info objectForKey:UIImagePickerControllerEditedImage];
[self.superVC dismissViewControllerAnimated:YES completion:^{
[self.delegate UUInputFunctionView:self sendPicture:editImage];
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self.superVC dismissViewControllerAnimated:YES completion:nil];
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
You can’t perform that action at this time.
