123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- //
- // EMChatBar.m
- // ChatDemo-UI3.0
- //
- // Created by XieYajie on 2019/1/25.
- // Copyright © 2019 XieYajie. All rights reserved.
- //
- #import "EMChatBar.h"
- #import "AudioShowView.h"
- #import "SmartBarModel.h"
- #import "NoticeNewGroupMenuCell.h"
- #define ktextViewMinHeight 40
- #define ktextViewMaxHeight 120
- @interface EMChatBar()<UITextViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
- @property (nonatomic) CGFloat version;
- @property (nonatomic) CGFloat previousTextViewContentHeight;
- @property (nonatomic) CGFloat keyHeight;
- @property (nonatomic) CGFloat cusviewHeight;
- @property (nonatomic, strong) UIButton *selectedButton;
- @property (nonatomic, strong) UIButton *speakBtn;
- @property (nonatomic, strong) UIButton *audioButton;
- @property (nonatomic, strong) UIButton *moreButton;
- @property (nonatomic, strong) UIButton *emojiButton;
- @property (nonatomic, strong) UIView *currentMoreView;
- @property (nonatomic, copy) NSMutableArray * listArray;
- @property (strong,nonatomic) UICollectionView *collectionView;
- @property (nonatomic, assign) BOOL isInput;
- @end
- @implementation EMChatBar
- - (instancetype)init
- {
- self = [super init];
- if (self) {
- _version = [[[UIDevice currentDevice] systemVersion] floatValue];
- _previousTextViewContentHeight = ktextViewMinHeight;
- [self _setupSubviews];
- }
-
- return self;
- }
- #pragma mark - Subviews
- - (void)_setupSubviews
- {
- self.keyHeight = 56;
- self.cusviewHeight = 0;
- self.backgroundColor = UIColorHex(f5f5f5);
- UIView *line = [[UIView alloc] init];
- line.backgroundColor = UIColorHex(E4E4E4);
- [self addSubview:line];
- [line mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.left.right.equalTo(self);
- make.height.equalTo(@0.5);
- }];
- self.textView = [[EMTextView alloc] init];
- self.textView.delegate = self;
- self.textView.placeholder = @"";
- self.textView.font = [UIFont systemFontOfSize:16];
- self.textView.returnKeyType = UIReturnKeySend;
- self.textView.backgroundColor = [UIColor whiteColor];
- self.textView.layer.cornerRadius = 5.f;
- self.textView.layer.masksToBounds = YES;
- [self addSubview:self.textView];
- [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.mas_top).offset(8);
- make.height.mas_equalTo(ktextViewMinHeight);
- make.left.equalTo(self.mas_left).offset(57);
- make.right.equalTo(self.mas_right).offset(-99);
- }];
- self.audioButton = [[UIButton alloc] init];
- [self.audioButton setImage:[UIImage imageNamed:@"chatmsg_detailchat_yy_icon"] forState:UIControlStateNormal];
- [self.audioButton addTarget:self action:@selector(audioButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:self.audioButton];
- [self.audioButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.bottom.mas_equalTo(self.textView.mas_bottom).mas_offset(-6.5);
- // make.centerY.equalTo(self.textView.mas_centerY);
- make.left.mas_offset(15);
- make.size.mas_offset(CGSizeMake(27, 27));
- }];
-
- self.moreButton = [[UIButton alloc] init];
- [self.moreButton setImage:[UIImage imageNamed:@"chatmsg_detailchat_add_icon"] forState:UIControlStateNormal];
- [self.moreButton addTarget:self action:@selector(moreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:self.moreButton];
- [self.moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
- // make.centerY.equalTo(self.textView.mas_centerY);
- make.bottom.mas_equalTo(self.textView.mas_bottom).mas_offset(-6.5);
- make.right.mas_offset(-15);
- make.size.mas_offset(CGSizeMake(27, 27));
- }];
-
- self.emojiButton = [[UIButton alloc] init];
- [self.emojiButton setImage:[UIImage imageNamed:@"chatmsg_detailchat_bq_icon"] forState:UIControlStateNormal];
- [self.emojiButton addTarget:self action:@selector(emoticonButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:self.emojiButton];
- [self.emojiButton mas_makeConstraints:^(MASConstraintMaker *make) {
- // make.centerY.equalTo(self.textView.mas_centerY);
- make.bottom.mas_equalTo(self.textView.mas_bottom).mas_offset(-6.5);
- make.right.equalTo(self.moreButton.mas_left).offset(-15);
- make.size.mas_offset(CGSizeMake(27, 27));
- }];
-
- self.speakBtn = [[UIButton alloc] init];
- NSMutableAttributedString *startstring = [[NSMutableAttributedString alloc] initWithString:@"按住 说话" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 17],NSForegroundColorAttributeName: [UIColor colorWithRed:10/255.0 green:10/255.0 blue:10/255.0 alpha:1.0]}];
- NSMutableAttributedString *endstring = [[NSMutableAttributedString alloc] initWithString:@"松开 结束" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 17],NSForegroundColorAttributeName: [UIColor colorWithRed:10/255.0 green:10/255.0 blue:10/255.0 alpha:1.0]}];
- [self.speakBtn setAttributedTitle:startstring forState:UIControlStateNormal];
- [self.speakBtn setAttributedTitle:endstring forState:UIControlStateHighlighted];
- self.speakBtn.backgroundColor = [UIColor whiteColor];
- self.speakBtn.layer.cornerRadius = 5.f;
- self.speakBtn.layer.masksToBounds = YES;
- [self addSubview:self.speakBtn];
- [self.speakBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.mas_equalTo(self.textView);
- }];
- self.speakBtn.hidden = YES;
- UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
- longPress.minimumPressDuration = 0;
- [self.speakBtn addGestureRecognizer:longPress];
- }
- - (void)longPress:(UILongPressGestureRecognizer *)gestureRecognizer
- {
- CGPoint point = [gestureRecognizer locationInView:self];
- NSMutableAttributedString *startstring = [[NSMutableAttributedString alloc] initWithString:@"按住 说话" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 17],NSForegroundColorAttributeName: [UIColor colorWithRed:10/255.0 green:10/255.0 blue:10/255.0 alpha:1.0]}];
- NSMutableAttributedString *endstring = [[NSMutableAttributedString alloc] initWithString:@"松开 发送" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 17],NSForegroundColorAttributeName: [UIColor colorWithRed:10/255.0 green:10/255.0 blue:10/255.0 alpha:1.0]}];
-
- if(gestureRecognizer.state == UIGestureRecognizerStateBegan) {
- [self.speakBtn setAttributedTitle:endstring forState:UIControlStateNormal];
- if (self.recordAudioView) {
- [[AudioShowView sharedAudioShowView] show];
- [self.recordAudioView _startRecord];
- [AudioShowView sharedAudioShowView].recorder = [self.recordAudioView getRecorder];
- }
- } else if(gestureRecognizer.state == UIGestureRecognizerStateEnded) {
- [self.speakBtn setAttributedTitle:startstring forState:UIControlStateNormal];
- if (self.recordAudioView) {
- [[AudioShowView sharedAudioShowView] dismiss];
- [self.recordAudioView _stopRecord];
- }
- } else if(gestureRecognizer.state == UIGestureRecognizerStateChanged) {
- if (![self.speakBtn.layer containsPoint:point]) {
- [self.speakBtn setAttributedTitle:endstring forState:UIControlStateNormal];
- if (self.recordAudioView) {
- [[AudioShowView sharedAudioShowView] dismiss];
- [self.recordAudioView _cancelRecord];
- }
- }
- } else if (gestureRecognizer.state == UIGestureRecognizerStateFailed) {
- NSLog(@"失败");
- } else if (gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
- NSLog(@"取消");
- }
- if (self.delegate && [self.delegate respondsToSelector:@selector(chatBarDidShowMoreViewAction:)]) {
- [self.delegate chatBarDidShowMoreViewAction:NO];
- }
- }
- - (void)clearAllView
- {
- [self.emojiButton setImage:[UIImage imageNamed:@"chatmsg_detailchat_bq_icon"] forState:UIControlStateNormal];
- [self.audioButton setImage:[UIImage imageNamed:@"chatmsg_detailchat_yy_icon"] forState:UIControlStateNormal];
- [self.moreButton setImage:[UIImage imageNamed:@"chatmsg_detailchat_add_icon"] forState:UIControlStateNormal];
- self.speakBtn.hidden = YES;
- self.textView.hidden = NO;
- }
- #pragma mark - UITextViewDelegate
- - (BOOL)textViewShouldBeginEditing:(UITextView *)textView
- {
- if (self.currentMoreView) {
- [self clearAllView];
- [self.currentMoreView removeFromSuperview];
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(self.keyHeight);
- }];
- }
-
- for (UIButton *button in self.buttonArray) {
- if (button.isSelected) {
- button.selected = NO;
- break;
- }
- }
-
- return YES;
- }
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
- {
- if (self.delegate && [self.delegate respondsToSelector:@selector(inputView:shouldChangeTextInRange:replacementText:)]) {
- return [self.delegate inputView:self.textView shouldChangeTextInRange:range replacementText:text];
- }
-
- return YES;
- }
- - (void)textViewDidChange:(UITextView *)textView
- {
- [self _updatetextViewHeight];
-
- if (self.delegate && [self.delegate respondsToSelector:@selector(inputViewDidChange:)]) {
- [self.delegate inputViewDidChange:self.textView];
- }
- }
- #pragma mark - Private
- - (CGFloat)_gettextViewContontHeight
- {
- if (self.version >= 7.0) {
- return ceilf([self.textView sizeThatFits:self.textView.frame.size].height);
- } else {
- return self.textView.contentSize.height;
- }
- }
- - (void)_updatetextViewHeight
- {
- CGFloat height = [self _gettextViewContontHeight];
- if (height < ktextViewMinHeight) {
- height = ktextViewMinHeight;
- }
- if (height > ktextViewMaxHeight) {
- height = ktextViewMaxHeight;
- }
- self.keyHeight = height + 16;
- if (height == self.previousTextViewContentHeight) {
- return;
- }
-
- self.previousTextViewContentHeight = height;
- [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(height);
- }];
- if (self.currentMoreView) {
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(self.keyHeight + self.cusviewHeight);
- }];
- }else{
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(self.keyHeight);
- }];
- }
- }
- #pragma mark - Public
- - (void)clearInputViewText
- {
- self.textView.text = @"";
- [self _updatetextViewHeight];
- }
- - (void)inputViewAppendText:(NSString *)aText
- {
- if ([aText length] > 0) {
- self.textView.text = [NSString stringWithFormat:@"%@%@", self.textView.text, aText];
- [self _updatetextViewHeight];
- }
- }
- - (void)clearMoreViewAndSelectedButton
- {
- if (self.currentMoreView) {
- [self.currentMoreView removeFromSuperview];
- self.currentMoreView = nil;
- [self clearAllView];
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(self.keyHeight);
- }];
- }
- if (self.selectedButton) {
- self.selectedButton.selected = NO;
- self.selectedButton = nil;
- }
- }
- #pragma mark - Action
- - (void)_buttonAction:(UIButton *)aButton
- {
- [self.textView resignFirstResponder];
-
- aButton.selected = !aButton.selected;
- if (self.currentMoreView) {
- [self.currentMoreView removeFromSuperview];
- self.currentMoreView = nil;
- }
-
- if (self.selectedButton != aButton) {
- self.selectedButton.selected = NO;
- self.selectedButton = nil;
- self.selectedButton = aButton;
- } else {
- self.selectedButton = nil;
- }
-
- if (aButton.selected) {
- self.selectedButton = aButton;
- }
- [self clearAllView];
- }
- - (void)audioButtonAction:(UIButton *)aButton
- {
- [self _buttonAction:aButton];
- [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(@40);
- }];
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(@56);
- }];
- if (aButton.selected) {
- [aButton setImage:[UIImage imageNamed:@"chatmsg_detailchat_jp_icon"] forState:UIControlStateNormal];
- self.textView.hidden = YES;
- self.speakBtn.hidden = NO;
- }else{
- [aButton setImage:[UIImage imageNamed:@"chatmsg_detailchat_yy_icon"] forState:UIControlStateNormal];
- self.speakBtn.hidden = YES;
- self.textView.hidden = NO;
- [self.textView becomeFirstResponder];
- }
- }
- - (void)emoticonButtonAction:(UIButton *)aButton
- {
- [self _buttonAction:aButton];
- if (aButton.selected) {
- [aButton setImage:[UIImage imageNamed:@"chatmsg_detailchat_jp_icon"] forState:UIControlStateNormal];
- if (self.moreEmoticonView) {
- self.currentMoreView = self.moreEmoticonView;
- [self addSubview:self.moreEmoticonView];
- [self.moreEmoticonView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self);
- make.right.equalTo(self);
- if (@available(iOS 11.0, *)) {
- make.bottom.equalTo(self.mas_safeAreaLayoutGuideBottom);
- } else {
- make.bottom.equalTo(self.mas_bottom);
- }
- make.height.mas_equalTo(self.moreEmoticonView.viewHeight);
- }];
- self.cusviewHeight = self.moreEmoticonView.viewHeight;
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(self.keyHeight + self.cusviewHeight);
- }];
- }
- }else{
- [aButton setImage:[UIImage imageNamed:@"chatmsg_detailchat_bq_icon"] forState:UIControlStateNormal];
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(self.keyHeight);
- }];
- [self.textView becomeFirstResponder];
- }
- if (self.delegate && [self.delegate respondsToSelector:@selector(chatBarDidShowMoreViewAction:)]) {
- [self.delegate chatBarDidShowMoreViewAction:NO];
- }
- }
- //更多
- - (void)moreButtonAction:(UIButton *)aButton
- {
- [self _buttonAction:aButton];
- [self addSubview:self.collectionView];
- [self CreatelistView];
- self.currentMoreView = self.collectionView;
- [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self);
- make.top.equalTo(self.textView.mas_bottom).offset(8);
- make.right.equalTo(self);
- if (@available(iOS 11.0, *)) {
- make.bottom.equalTo(self.mas_safeAreaLayoutGuideBottom);
- } else {
- make.bottom.equalTo(self.mas_bottom);
- }
- }];
- if (self.cusviewHeight > 0) {
- self.collectionView.hidden = YES;
- [self.textView becomeFirstResponder];
- self.cusviewHeight = 0;
- if (self.delegate && [self.delegate respondsToSelector:@selector(chatBarDidShowMoreViewAction:)]) {
- [self.delegate chatBarDidShowMoreViewAction:YES];
- }
- // [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
- // make.height.mas_equalTo(self.cusviewHeight);
- // }];
- }else{
- self.collectionView.hidden = NO;
- self.cusviewHeight = 215.f * HEIGHT_SCALE;
- if (self.delegate && [self.delegate respondsToSelector:@selector(chatBarDidShowMoreViewAction:)]) {
- [self.delegate chatBarDidShowMoreViewAction:NO];
- }
- // [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
- // make.height.mas_equalTo(self.cusviewHeight);
- // }];
- }
- [self mas_updateConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(self.keyHeight + self.cusviewHeight);
- }];
- }
- - (void)CreatelistView
- {
- [self.listArray removeAllObjects];
- NSArray * array = @[@{@"name":@"图片",@"image":@"chatmsg_notice_photo_icon",@"type":@(0)},
- @{@"name":@"拍摄",@"image":@"chatmsg_notice_xiangji_icon",@"type":@(1)},
- @{@"name":@"我的笔记",@"image":@"chatmsg_notice_biji_icon",@"type":@(2)},
- @{@"name":@"我的收藏",@"image":@"chatmsg_notice_sc_icon",@"type":@(3)},
- @{@"name":@"文件",@"image":@"chatmsg_notice_file_icon",@"type":@(4)}];
- for (NSDictionary * dict in array) {
- SmartBarModel * model = [[SmartBarModel alloc] init];
- model.name = [dict objectForKey:@"name"];
- model.image = [dict objectForKey:@"image"];
- model.type = [[dict objectForKey:@"type"] intValue];
- [self.listArray addObject:model];
- }
- [self.collectionView reloadData];
- }
- #pragma mark - UICollectionView
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return self.listArray.count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- SmartBarModel * model = [self.listArray objectAtIndex:indexPath.item];
- NoticeNewGroupMenuCell *cell = [ collectionView dequeueReusableCellWithReuseIdentifier:@"NoticeNewGroupMenuCell" forIndexPath:indexPath];
- cell.cell0ImgView.image = [UIImage imageNamed:model.image];
- cell.cell0TitleLabel.text = model.name;
- return cell;
- }
- /****************************************************/
- #pragma mark --UICollectionViewDelegateFlowLayout
- /****************************************************/
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- CGFloat width = (SCREEN_WIDTH - 60 - 75) * 0.25;
- CGFloat height = Height_SCALE(100);
- return CGSizeMake(width, height);
- }
- //设置每个item的UIEdgeInsets
- - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
- {
- return UIEdgeInsetsMake(0, 0, 0, 0);
- }
- //设置每个item水平间距
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
- {
- return 25.f;
- }
- //设置每个item垂直间距
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
- {
- return 0.f;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
- [collectionView deselectItemAtIndexPath:indexPath animated:YES];
- SmartBarModel * model = [self.listArray objectAtIndex:indexPath.item];
- if(self.delegate && [self.delegate respondsToSelector:@selector(chatBarClickMoreType:)]){
- [self.delegate chatBarClickMoreType:model.type];
- }
- }
- #pragma mark - CusView
- - (UICollectionView *)collectionView
- {
- if (!_collectionView) {
- _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
- [_collectionView registerNib:[UINib nibWithNibName:@"NoticeNewGroupMenuCell" bundle:nil] forCellWithReuseIdentifier:@"NoticeNewGroupMenuCell"];
- _collectionView.backgroundColor = UIColorHex(#F4F4F6);
- _collectionView.showsVerticalScrollIndicator = NO;
- _collectionView.showsHorizontalScrollIndicator = NO;
- _collectionView.contentInset = UIEdgeInsetsMake(0, 30, 0, 30);
- _collectionView.alwaysBounceVertical = YES;
- _collectionView.scrollEnabled = NO;
- _collectionView.dataSource = self;
- _collectionView.delegate = self;
- }
- return _collectionView;
- }
- - (NSMutableArray *)listArray
- {
- if (!_listArray) {
- _listArray = [NSMutableArray array];
- }
- return _listArray;
- }
- @end
|