| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- //
- // DKMessageInputView.m
- // MessageDisplayExample
- //
- // Created by HUAJIE-1 on 14-4-24.
- // Copyright (c) 2014年 曾宪华 开发团队(http://iyilunba.com ) 本人QQ:543413507 本人QQ群(142557668). All rights reserved.
- //
- #import <QuartzCore/QuartzCore.h>
- #import "DKMessageInputView.h"
- #import "NSString+MessageInputView.h"
- #import "DKMacro.h"
- #import "DKConfigurationHelper.h"
- @interface DKMessageInputView () <UITextViewDelegate>
- @property (nonatomic, weak ,readwrite) UIButton *voiceChangeButton;
- @property (nonatomic, weak,readwrite) UIButton *cameraButton;
- @property (nonatomic, weak,readwrite ) UIButton *holdDownButton;
- @property(nonatomic,weak,readwrite )UIButton *photoButton;
- @property (nonatomic, weak, readwrite) UIButton *keyboardButton;
- @property (nonatomic, weak, readwrite) UIButton *positionButton;
- /**
- * 是否取消錄音
- */
- @property (nonatomic, assign, readwrite) BOOL isCancelled;
- /**
- * 是否正在錄音
- */
- @property (nonatomic, assign, readwrite) BOOL isRecording;
- /**
- * 在切换语音和文本消息的时候,需要保存原本已经输入的文本,这样达到一个好的UE
- */
- @property (nonatomic, copy) NSString *inputedText;
- /**
- * 输入框内的所有按钮,点击事件所触发的方法
- *
- * @param sender 被点击的按钮对象
- */
- - (void)messageStyleButtonClicked:(UIButton *)sender;
- /**
- * 当录音按钮被按下所触发的事件,这时候是开始录音
- */
- - (void)holdDownButtonTouchDown;
- /**
- * 当手指在录音按钮范围之外离开屏幕所触发的事件,这时候是取消录音
- */
- - (void)holdDownButtonTouchUpOutside;
- /**
- * 当手指在录音按钮范围之内离开屏幕所触发的事件,这时候是完成录音
- */
- - (void)holdDownButtonTouchUpInside;
- /**
- * 当手指滑动到录音按钮的范围之外所触发的事件
- */
- - (void)holdDownDragOutside;
- /**
- * 当手指滑动到录音按钮的范围之内所触发的时间
- */
- - (void)holdDownDragInside;
- #pragma mark - layout subViews UI
- /**
- * 根据正常显示和高亮状态创建一个按钮对象
- *
- * @param image 正常显示图
- * @param hlImage 高亮显示图
- *
- * @return 返回按钮对象
- */
- - (UIButton *)createButtonWithImage:(UIImage *)image HLImage:(UIImage *)hlImage ;
- /**
- * 根据输入框的样式类型配置输入框的样式和UI布局
- *
- * @param style 输入框样式类型
- */
- - (void)setupMessageInputViewBarWithStyle:(DKMessageInputViewStyle)style ;
- /**
- * 配置默认参数
- */
- - (void)setup ;
- @end
- @implementation DKMessageInputView
- #pragma mark - Action
- - (void)messageStyleButtonClicked:(UIButton *)sender {
- NSInteger index = sender.tag;
- switch (index) {
- case 0: {
- if ([self.delegate respondsToSelector:@selector(saveLog)]) {
- [self.delegate saveLog];
- }
- break;
- }
- case 1: {
- _voiceButton.hidden=YES;
- _switchButton.hidden=YES;
- _saveButton.hidden=NO;
-
- break;
- }
-
- default:
- break;
- }
- }
- - (void)holdDownButtonTouchDown {
- self.isCancelled = NO;
- self.isRecording = NO;
- if ([self.delegate respondsToSelector:@selector(prepareRecordingVoiceActionWithCompletion:)]) {
- WEAKSELF
-
- //這邊回調 return 的 YES, 或 NO, 可以讓底層知道該次錄音是否成功, 進而處理無用的 record 對象
- [self.delegate prepareRecordingVoiceActionWithCompletion:^BOOL{
- STRONGSELF
-
- //這邊要判斷回調回來的時候, 使用者是不是已經早就鬆開手了
- if (strongSelf && !strongSelf.isCancelled) {
- strongSelf.isRecording = YES;
- [strongSelf.delegate didStartRecordingVoiceAction];
- return YES;
- } else {
- return NO;
- }
- }];
- }
- }
- - (void)holdDownButtonTouchUpOutside {
-
- //如果已經開始錄音了, 才需要做取消的動作, 否則只要切換 isCancelled, 不讓錄音開始.
- if (self.isRecording) {
- if ([self.delegate respondsToSelector:@selector(didCancelRecordingVoiceAction)]) {
- [self.delegate didCancelRecordingVoiceAction];
- }
- } else {
- self.isCancelled = YES;
- }
- }
- - (void)holdDownButtonTouchUpInside {
-
- //如果已經開始錄音了, 才需要做結束的動作, 否則只要切換 isCancelled, 不讓錄音開始.
- if (self.isRecording) {
- if ([self.delegate respondsToSelector:@selector(didFinishRecordingVoiceAction)]) {
- [self.delegate didFinishRecordingVoiceAction];
- }
- } else {
- self.isCancelled = YES;
- }
- }
- - (void)holdDownDragOutside {
-
- //如果已經開始錄音了, 才需要做拖曳出去的動作, 否則只要切換 isCancelled, 不讓錄音開始.
- if (self.isRecording) {
- if ([self.delegate respondsToSelector:@selector(didDragOutsideAction)]) {
- [self.delegate didDragOutsideAction];
- }
- } else {
- self.isCancelled = YES;
- }
- }
- - (void)holdDownDragInside {
-
- //如果已經開始錄音了, 才需要做拖曳回來的動作, 否則只要切換 isCancelled, 不讓錄音開始.
- if (self.isRecording) {
- if ([self.delegate respondsToSelector:@selector(didDragInsideAction)]) {
- [self.delegate didDragInsideAction];
- }
- } else {
- self.isCancelled = YES;
- }
- }
- #pragma mark - layout subViews UI
- - (UIButton *)createButtonWithImage:(UIImage *)image HLImage:(UIImage *)hlImage {
- UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, [DKMessageInputView textViewLineHeight], [DKMessageInputView textViewLineHeight])];
- if (image)
- [button setBackgroundImage:image forState:UIControlStateNormal];
- if (hlImage)
- [button setBackgroundImage:hlImage forState:UIControlStateHighlighted];
-
- return button;
- }
- #pragma mark - layout subViews UI
- - (UIButton *)createVoiceButtonWithImage:(UIImage *)image HLImage:(UIImage *)hlImage {
- UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 5,200, 36)];
- button.layer.borderWidth=1;
- button.layer.borderColor=[UIColor lightGrayColor].CGColor;
- button.layer.cornerRadius=10;
- // if (image)
- // [button setBackgroundImage:image forState:UIControlStateNormal];
- // if (hlImage)
- // [button setBackgroundImage:hlImage forState:UIControlStateHighlighted];
-
- return button;
- }
- #pragma mark - layout subViews UI
- - (UIButton *)createSaveButtonWithImage:(UIImage *)image HLImage:(UIImage *)hlImage {
- float width =[DKMessageInputView textViewLineHeight]*image.size.width/image.size.height;
- UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake((Screen_Width-width)/2, 5,width, [DKMessageInputView textViewLineHeight])];
- if (image)
- [button setBackgroundImage:image forState:UIControlStateNormal];
- if (hlImage)
- [button setBackgroundImage:hlImage forState:UIControlStateHighlighted];
-
- return button;
- }
- - (void)setupMessageInputViewBarWithStyle:(DKMessageInputViewStyle)style {
- // 配置输入工具条的样式和布局
-
- // 需要显示按钮的总宽度,包括间隔在内
- CGFloat allButtonWidth = 0.0;
-
-
- // 垂直间隔
- CGFloat verticalPadding = 5;
-
- // 输入框
- CGFloat textViewLeftMargin = ((style == DKMessageInputViewStyleFlat) ? 6.0 : 4.0);
-
- // 每个按钮统一使用的frame变量
- CGRect buttonFrame;
-
-
- // 允许发送语音
- NSString *voiceNormalImageName = [[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewVoiceNormalImageNameKey];
- if (!voiceNormalImageName) {
- voiceNormalImageName = @"keyboard";
- }
- NSString *voiceHLImageName = [[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewVoiceHLImageNameKey];
- if (!voiceHLImageName) {
- voiceHLImageName = @"keyboard";
- }
- NSString *voiceSaveImageName=@"log_save";
- _saveButton =[self createSaveButtonWithImage:[UIImage imageNamed:voiceSaveImageName] HLImage:[UIImage imageNamed:voiceSaveImageName]];
- _saveButton.tag = 0;
- [_saveButton addTarget:self action:@selector(messageStyleButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:_saveButton];
- _switchButton = [self createButtonWithImage:[UIImage imageNamed:voiceNormalImageName] HLImage:[UIImage imageNamed:voiceHLImageName]];
- [_switchButton addTarget:self action:@selector(messageStyleButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
- _switchButton.tag = 1;
- _switchButton.hidden=YES;
- buttonFrame = _switchButton.frame;
- buttonFrame.origin = CGPointMake(20, verticalPadding);
- _switchButton.frame = buttonFrame;
- [self addSubview:_switchButton];
- allButtonWidth += CGRectGetMaxX(buttonFrame);
- textViewLeftMargin += CGRectGetMaxX(buttonFrame);
- self.voiceChangeButton = _switchButton;
-
-
- // // 输入框的高度和宽度
- // CGFloat width = CGRectGetWidth(self.bounds) - (allButtonWidth ? allButtonWidth : (textViewLeftMargin * 2));
-
- // 配置不同iOS SDK版本的样式
- switch (style) {
- case DKMessageInputViewStyleFlat: {
- UIColor *inputBackgroundColor = [DKConfigurationHelper appearance].messageInputViewStyle[kDKMessageInputViewBackgroundColorKey];
- NSString *inputViewBackgroundImageName = nil;
- if (!inputBackgroundColor) {
- inputViewBackgroundImageName = [[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewBackgroundImageNameKey];
- if (!inputViewBackgroundImageName) {
- inputViewBackgroundImageName = @"input-bar-flat";
- }
- }
-
- UIColor *borderColor = [[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewBorderColorKey];
- if (!borderColor) {
- borderColor=LineBackgroundColor;
-
- }
- CGFloat borderWidth = [[[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewBorderWidthKey] floatValue];
- if (borderWidth == 0) {
- borderWidth = 0.65f;
- }
- CGFloat cornerRadius = [[[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewCornerRadiusKey] floatValue];
- if (cornerRadius == 0) {
- cornerRadius = 6.0f;
- }
-
- if (inputBackgroundColor) {
- self.backgroundColor = inputBackgroundColor;
- } else {
- self.image = [[UIImage imageNamed:inputViewBackgroundImageName] resizableImageWithCapInsets:UIEdgeInsetsMake(2.0f, 0.0f, 0.0f, 0.0f)
- resizingMode:UIImageResizingModeTile];
- }
- break;
- }
- default:
- break;
- }
-
- // 如果是可以发送语音的,那就需要一个按钮录音的按钮,事件可以在外部添加
-
- NSString *voiceHolderImageName = [[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewVoiceHolderImageNameKey];
- voiceHolderImageName = @"voicebtn_black";
- NSString *voiceHolderHLImageName = [[DKConfigurationHelper appearance].messageInputViewStyle objectForKey:kDKMessageInputViewVoiceHolderHLImageNameKey];
-
- voiceHolderHLImageName = @"voicebtn_blackhl";
-
- UIEdgeInsets edgeInsets = UIEdgeInsetsMake(9, 9, 9, 9);
- _voiceButton = [self createVoiceButtonWithImage:DK_STRETCH_IMAGE([UIImage imageNamed:voiceHolderImageName], edgeInsets) HLImage:DK_STRETCH_IMAGE([UIImage imageNamed:voiceHolderHLImageName], edgeInsets)];
- _voiceButton.hidden=YES;
- //按住说话
- [_voiceButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
-
- [_voiceButton setTitle:@"按住说话" forState:UIControlStateNormal];
-
- [_voiceButton setTitle:@"放开发送" forState:UIControlStateHighlighted];
- CGRect btnFrame = CGRectMake(CGRectGetMaxX(_switchButton.frame)+30,5,Screen_Width-CGRectGetMaxX(_switchButton.frame)-60,36);
- _voiceButton.frame = btnFrame;
- _voiceButton.tag=2;
- [_voiceButton addTarget:self action:@selector(holdDownButtonTouchDown) forControlEvents:UIControlEventTouchDown];
- [_voiceButton addTarget:self action:@selector(holdDownButtonTouchUpOutside) forControlEvents:UIControlEventTouchUpOutside];
- [_voiceButton addTarget:self action:@selector(holdDownButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
- [_voiceButton addTarget:self action:@selector(holdDownDragOutside) forControlEvents:UIControlEventTouchDragExit];
- [_voiceButton addTarget:self action:@selector(holdDownDragInside) forControlEvents:UIControlEventTouchDragEnter];
- [self addSubview:_voiceButton];
- self.holdDownButton =_voiceButton;
- }
- #pragma mark - Life cycle
- - (void)setup {
- // 配置自适应
- self.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin);
- self.opaque = YES;
- // 由于继承UIImageView,所以需要这个属性设置
- self.userInteractionEnabled = YES;
- _allowsPosition=YES;
- _messageInputViewStyle = DKMessageInputViewStyleFlat;
- }
- - (void)awakeFromNib {
- [super awakeFromNib];
- [self setup];
- }
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- [self setup];
- }
- return self;
- }
- - (void)dealloc {
- self.inputedText = nil;
-
- _voiceChangeButton = nil;
- _cameraButton = nil;
- _photoButton = nil;
- _holdDownButton = nil;
- _positionButton=nil;
- _keyboardButton=nil;
- }
- - (void)willMoveToSuperview:(UIView *)newSuperview {
- // 当别的地方需要add的时候,就会调用这里
- if (newSuperview) {
- [self setupMessageInputViewBarWithStyle:self.messageInputViewStyle];
- }
- }
- + (CGFloat)textViewLineHeight {
- return 36.0f; // for fontSize 16.0f
- }
- + (CGFloat)textViewLineWidth {
- return Screen_Width-40; // for fontSize 16.0f
- }
- + (CGFloat)maxLines {
- return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) ? 3.0f : 8.0f;
- }
- + (CGFloat)maDKeight {
- return ([DKMessageInputView maxLines] + 1.0f) * [DKMessageInputView textViewLineHeight];
- }
- @end
|