SmartBar.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // SmartBar.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2019/12/14.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "SmartBar.h"
  9. #import "GHRefreshCollectionView.h"
  10. #import "NoticeNewGroupMenuCell.h"
  11. #import "ZLCollectionViewFlowLayout.h"
  12. #import "TDButton.h"
  13. #define BARHEIGHT 42.5f
  14. @interface SmartBar()<UICollectionViewDelegate,UICollectionViewDataSource>
  15. @property (nonatomic, copy) NSMutableArray <SmartBarModel>* listArray;
  16. @property (nonatomic, assign)CGFloat SmartHeight;
  17. @property (strong,nonatomic) UICollectionView *collectionView;
  18. @property (nonatomic,strong) UIView *chatBar;
  19. @property (nonatomic,strong) TDButton *addBtn;
  20. @property (nonatomic,strong) TDButton *keyBtn;
  21. @property (nonatomic,assign) BOOL keyBoardIsShow;
  22. @end
  23. @implementation SmartBar
  24. - (instancetype)initHeight:(CGFloat)height AddBarArray:(NSMutableArray<SmartBarModel> *)array;
  25. {
  26. self = [super init];
  27. if (self) {
  28. self.listArray = array;
  29. self.SmartHeight = height;
  30. [self addBarSubView];
  31. }
  32. return self;
  33. }
  34. - (instancetype)init
  35. {
  36. self = [super init];
  37. if (self) {
  38. self.SmartHeight = BARHEIGHT;
  39. [self addBarSubView];
  40. }
  41. return self;
  42. }
  43. - (void)addBarSubView
  44. {
  45. [self addSubview:self.chatBar];
  46. [self.chatBar addSubview:self.addBtn];
  47. [self.chatBar addSubview:self.keyBtn];
  48. [self.chatBar mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.left.top.right.mas_equalTo(self);
  50. make.height.mas_offset(self.SmartHeight);
  51. }];
  52. UILabel * topLineL = [UILabel new];
  53. topLineL.backgroundColor = UIColorHex(B4B4B4);
  54. UILabel * botLineL = [UILabel new];
  55. botLineL.backgroundColor = UIColorHex(B4B4B4);
  56. UILabel * hLineL = [UILabel new];
  57. hLineL.backgroundColor = UIColorHex(B4B4B4);
  58. [self.chatBar addSubview:topLineL];
  59. [self.chatBar addSubview:botLineL];
  60. [self.chatBar addSubview:hLineL];
  61. [topLineL mas_makeConstraints:^(MASConstraintMaker *make) {
  62. make.left.top.right.mas_equalTo(self.chatBar);
  63. make.height.mas_offset(0.5);
  64. }];
  65. [botLineL mas_makeConstraints:^(MASConstraintMaker *make) {
  66. make.left.bottom.right.mas_equalTo(self.chatBar);
  67. make.height.mas_offset(0.5);
  68. }];
  69. [hLineL mas_makeConstraints:^(MASConstraintMaker *make) {
  70. make.top.bottom.mas_equalTo(self.chatBar);
  71. make.width.mas_offset(0.5);
  72. make.right.mas_equalTo(self.chatBar.mas_right).offset(-50);
  73. }];
  74. [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  75. make.top.mas_offset(10);
  76. make.left.mas_offset(22);
  77. make.size.mas_offset(CGSizeMake(22, 22));
  78. }];
  79. [self.keyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.top.mas_offset(10.5);
  81. make.right.mas_offset(-13.5);
  82. make.size.mas_offset(CGSizeMake(23.5, 21.5));
  83. }];
  84. [self CreatelistView];
  85. WS(weakSelf);
  86. [self.addBtn setAction:^{
  87. // if (weakSelf.collectionView.isHidden) {
  88. // weakSelf.collectionView.hidden = NO;
  89. // }else{
  90. // weakSelf.collectionView.hidden = YES;
  91. // }
  92. weakSelf.collectionView.hidden = NO;
  93. if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(didSelectBarAddBtnIsShow:)]){
  94. [weakSelf.delegate didSelectBarAddBtnIsShow:YES];
  95. }
  96. [weakSelf mas_updateConstraints:^(MASConstraintMaker *make) {
  97. make.height.mas_offset(weakSelf.SmartHeight * 2 + (weakSelf.collectionView.isHidden ? 0 : Height_SCALE(215)));
  98. }];
  99. }];
  100. [self.keyBtn setAction:^{
  101. [weakSelf downBar];
  102. if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(didSelectBarKeyBtnIsShow:)]){
  103. [weakSelf.delegate didSelectBarKeyBtnIsShow:!weakSelf.keyBoardIsShow];
  104. }
  105. }];
  106. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  107. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  108. }
  109. - (void)CreatelistView
  110. {
  111. [self addSubview:self.collectionView];
  112. self.collectionView.hidden = YES;
  113. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  114. make.left.bottom.right.mas_equalTo(self);
  115. make.top.mas_equalTo(self.chatBar.mas_bottom);
  116. }];
  117. if (self.listArray.count == 0) {
  118. NSArray * array = @[@{@"name":@"图片",@"image":@"chatmsg_notice_photo_icon",@"type":@(0)},
  119. @{@"name":@"拍摄",@"image":@"chatmsg_notice_xiangji_icon",@"type":@(1)},
  120. @{@"name":@"我的笔记",@"image":@"chatmsg_notice_biji_icon",@"type":@(2)},
  121. @{@"name":@"我的收藏",@"image":@"chatmsg_notice_sc_icon",@"type":@(3)},
  122. @{@"name":@"文件",@"image":@"chatmsg_notice_file_icon",@"type":@(4)}];
  123. for (NSDictionary * dict in array) {
  124. SmartBarModel * model = [[SmartBarModel alloc] init];
  125. model.name = [dict objectForKey:@"name"];
  126. model.image = [dict objectForKey:@"image"];
  127. model.type = [[dict objectForKey:@"type"] intValue];
  128. [self.listArray addObject:model];
  129. }
  130. }
  131. [self.collectionView reloadData];
  132. }
  133. - (void)downBar
  134. {
  135. if (!self.collectionView.hidden) {
  136. self.collectionView.hidden = YES;
  137. }
  138. [self.chatBar mas_updateConstraints:^(MASConstraintMaker *make) {
  139. make.height.mas_offset(self.SmartHeight);
  140. }];
  141. [self mas_updateConstraints:^(MASConstraintMaker *make) {
  142. make.height.mas_offset(self.SmartHeight);
  143. }];
  144. }
  145. #pragma mark - UICollectionView
  146. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  147. return self.listArray.count;
  148. }
  149. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  150. {
  151. SmartBarModel * model = [self.listArray objectAtIndex:indexPath.item];
  152. NoticeNewGroupMenuCell *cell = [ collectionView dequeueReusableCellWithReuseIdentifier:@"NoticeNewGroupMenuCell" forIndexPath:indexPath];
  153. cell.cell0ImgView.image = [UIImage imageNamed:model.image];
  154. cell.cell0TitleLabel.text = model.name;
  155. return cell;
  156. }
  157. /****************************************************/
  158. #pragma mark --UICollectionViewDelegateFlowLayout
  159. /****************************************************/
  160. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  161. {
  162. CGFloat width = (SCREEN_WIDTH - 60 - 75) * 0.25;
  163. CGFloat height = Height_SCALE(100);
  164. return CGSizeMake(width, height);
  165. }
  166. //设置每个item的UIEdgeInsets
  167. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  168. {
  169. return UIEdgeInsetsMake(0, 0, 0, 0);
  170. }
  171. //设置每个item水平间距
  172. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  173. {
  174. return 25.f;
  175. }
  176. //设置每个item垂直间距
  177. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  178. {
  179. return 0.f;
  180. }
  181. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  182. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  183. SmartBarModel * model = [self.listArray objectAtIndex:indexPath.item];
  184. if(self.delegate && [self.delegate respondsToSelector:@selector(didSelectBarWithType:)]){
  185. [self.delegate didSelectBarWithType:model.type];
  186. }
  187. }
  188. #pragma mark - CusView
  189. - (UICollectionView *)collectionView
  190. {
  191. if (!_collectionView) {
  192. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
  193. [_collectionView registerNib:[UINib nibWithNibName:@"NoticeNewGroupMenuCell" bundle:nil] forCellWithReuseIdentifier:@"NoticeNewGroupMenuCell"];
  194. _collectionView.backgroundColor = UIColorHex(#F4F4F6);
  195. _collectionView.showsVerticalScrollIndicator = NO;
  196. _collectionView.showsHorizontalScrollIndicator = NO;
  197. _collectionView.contentInset = UIEdgeInsetsMake(0, 30, 0, 30);
  198. _collectionView.alwaysBounceVertical = YES;
  199. _collectionView.scrollEnabled = NO;
  200. _collectionView.dataSource = self;
  201. _collectionView.delegate = self;
  202. }
  203. return _collectionView;
  204. }
  205. - (UIView *)chatBar
  206. {
  207. if (!_chatBar) {
  208. _chatBar = [UIView new];
  209. _chatBar.backgroundColor = UIColorHex(FAFAFA);
  210. }
  211. return _chatBar;
  212. }
  213. - (TDButton *)addBtn
  214. {
  215. if (!_addBtn) {
  216. _addBtn = [TDButton new];
  217. [_addBtn setImage:[UIImage imageNamed:@"smart_add"] forState:UIControlStateNormal];
  218. [_addBtn setCurrentButtonHotSize:CGSizeMake(BARHEIGHT, BARHEIGHT)];
  219. }
  220. return _addBtn;
  221. }
  222. - (TDButton *)keyBtn
  223. {
  224. if (!_keyBtn) {
  225. _keyBtn = [TDButton new];
  226. [_keyBtn setImage:[UIImage imageNamed:@"smart_key"] forState:UIControlStateNormal];
  227. [_keyBtn setCurrentButtonHotSize:CGSizeMake(BARHEIGHT, BARHEIGHT)];
  228. }
  229. return _keyBtn;
  230. }
  231. - (NSMutableArray *)listArray
  232. {
  233. if (!_listArray) {
  234. _listArray = [NSMutableArray array];
  235. }
  236. return _listArray;
  237. }
  238. #pragma mark - KeyBoard
  239. - (void)keyBoardWillShow:(NSNotification *)note
  240. {
  241. [self downBar];
  242. self.keyBoardIsShow = YES;
  243. }
  244. - (void)keyBoardWillHide:(NSNotification *)note
  245. {
  246. self.keyBoardIsShow = NO;
  247. }
  248. - (void)dealloc{
  249. [[NSNotificationCenter defaultCenter] removeObserver:self];
  250. }
  251. @end