BAGridView.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //
  2. // BAGridView.m
  3. // BAKit
  4. //
  5. // Created by boai on 2017/4/14.
  6. // Copyright © 2017年 boaihome. All rights reserved.
  7. //
  8. #import "BAGridView.h"
  9. #import "BAGridCollectionCell.h"
  10. #import "BAGridViewTypeTitleDescCell.h"
  11. #import "BAGridItemModel.h"
  12. #import "BAKit_ConfigurationDefine.h"
  13. static NSString * const kCellID = @"BAGridCollectionCell";
  14. static NSString * const kCellID2 = @"BAGridViewTypeTitleDescCell";
  15. @interface BAGridView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
  16. @property(nonatomic, strong) UICollectionView *collectionView;
  17. @property(nonatomic, assign) CGFloat gridItem_w;
  18. @property(nonatomic, assign) CGFloat ba_gridView_lineWidth;
  19. @property(nonatomic, weak) NSIndexPath *selectIndexPath;
  20. @end
  21. @implementation BAGridView
  22. - (instancetype)init
  23. {
  24. self = [super init];
  25. if (self) {
  26. [self setupUI];
  27. }
  28. return self;
  29. }
  30. - (instancetype)initWithFrame:(CGRect)frame
  31. {
  32. self = [super initWithFrame:frame];
  33. if (self) {
  34. [self setupUI];
  35. }
  36. return self;
  37. }
  38. - (instancetype)initWithCoder:(NSCoder *)aDecoder
  39. {
  40. if (self = [super initWithCoder:aDecoder])
  41. {
  42. [self setupUI];
  43. }
  44. return self;
  45. }
  46. /**
  47. 快速创建宫格
  48. @param gridViewType 样式
  49. @param dataArray 数据
  50. @param configurationBlock 配置回调
  51. @param block 点击事件回调
  52. @return BAGridView
  53. */
  54. + (instancetype)ba_creatGridViewWithGridViewType:(BAGridViewType)gridViewType
  55. dataArray:(NSArray <BAGridItemModel *>*)dataArray
  56. configurationBlock:(BAGridView_configurationBlock)configurationBlock
  57. block:(BAGridViewBlock)block
  58. {
  59. BAGridView *tempView = [[BAGridView alloc] init];
  60. if (configurationBlock)
  61. {
  62. configurationBlock(tempView);
  63. }
  64. tempView.gridViewType = gridViewType;
  65. tempView.dataArray = dataArray;
  66. tempView.ba_gridViewBlock = block;
  67. return tempView;
  68. }
  69. - (void)setupUI
  70. {
  71. self.backgroundColor = BAKit_Color_Clear_pod;
  72. self.collectionView.hidden = NO;
  73. // 默认配置
  74. self.gridViewType = BAGridViewTypeImageTitle;
  75. self.ba_gridView_rowCount = 4;
  76. self.ba_gridView_lineColor = BAKit_Color_Gray_10_pod;
  77. self.ba_gridView_lineWidth = BAKit_Flat_pod(0.5f);
  78. self.ba_gridView_titleColor = BAKit_Color_Black_pod;
  79. self.ba_gridView_titleDescColor = BAKit_Color_Gray_9_pod;
  80. self.ba_gridView_itemImageInset = 0;
  81. self.ba_gridView_backgroundColor = BAKit_Color_White_pod;
  82. }
  83. - (void)layoutSubviews
  84. {
  85. [super layoutSubviews];
  86. self.collectionView.frame = self.bounds;
  87. }
  88. #pragma mark - UICollectionViewDelegate, UICollectionViewDataSource
  89. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  90. {
  91. return self.dataArray.count;
  92. }
  93. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  94. {
  95. BAGridCollectionCell *cell;
  96. BAGridViewTypeTitleDescCell *cell2;
  97. [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
  98. if (self.gridViewType == BAGridViewTypeImageTitle)
  99. {
  100. cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
  101. cell.backgroundColor = BAKit_Color_Clear_pod;
  102. cell.model = self.dataArray[indexPath.row];
  103. cell.ba_gridView_titleColor = self.ba_gridView_titleColor;
  104. cell.ba_gridView_lineColor = self.ba_gridView_lineColor;
  105. cell.ba_gridView_lineWidth = self.ba_gridView_lineWidth;
  106. cell.ba_gridView_itemImageInset = self.ba_gridView_itemImageInset;
  107. cell.ba_gridView_titleFont = self.ba_gridView_titleFont;
  108. return cell;
  109. }
  110. else if (self.gridViewType == BAGridViewTypeTitleDesc)
  111. {
  112. cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:kCellID2 forIndexPath:indexPath];
  113. cell2.backgroundColor = BAKit_Color_Clear_pod;
  114. cell2.model = self.dataArray[indexPath.row];
  115. cell2.ba_gridView_titleColor = self.ba_gridView_titleColor;
  116. cell2.ba_gridView_titleDescColor = self.ba_gridView_titleDescColor;
  117. cell2.ba_gridView_lineColor = self.ba_gridView_lineColor;
  118. cell2.ba_gridView_lineWidth = self.ba_gridView_lineWidth;
  119. cell2.ba_gridView_itemImageInset = self.ba_gridView_itemImageInset;
  120. cell2.ba_gridView_titleFont = self.ba_gridView_titleFont;
  121. cell2.ba_gridView_titleDescFont = self.ba_gridView_titleDescFont;
  122. return cell2;
  123. }
  124. return [UICollectionViewCell new];
  125. }
  126. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  127. {
  128. // [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  129. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  130. // 选中之后的cell变颜色
  131. [self ba_updateCell:cell indexPath:indexPath selected:YES];
  132. BAGridItemModel *model = self.dataArray[indexPath.row];
  133. if (self.ba_gridViewBlock)
  134. {
  135. self.ba_gridViewBlock(model, indexPath);
  136. }
  137. }
  138. // 取消选中操作
  139. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
  140. {
  141. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  142. [self ba_updateCell:cell indexPath:indexPath selected:NO];
  143. }
  144. // 改变cell的背景颜色
  145. - (void)ba_updateCell:(id)cell
  146. indexPath:(NSIndexPath *)indexPath
  147. selected:(BOOL)selected
  148. {
  149. [UIView animateWithDuration:0.20f animations:^{
  150. ((UICollectionViewCell *)cell).backgroundColor = selected ? self.ba_gridView_selectedBackgroundColor : self.ba_gridView_backgroundColor;
  151. } completion:^(BOOL finished) {
  152. [UIView animateWithDuration:0.08f animations:^{
  153. ((UICollectionViewCell *)cell).backgroundColor = self.backgroundColor;
  154. }];
  155. }];
  156. self.selectIndexPath = indexPath;
  157. }
  158. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  159. {
  160. return (CGSizeMake(self.gridItem_w, self.ba_gridView_itemHeight));
  161. }
  162. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  163. {
  164. return UIEdgeInsetsMake(0, 0, 0, 0);
  165. }
  166. #pragma mark - setter / getter
  167. - (UICollectionView *)collectionView
  168. {
  169. if (!_collectionView)
  170. {
  171. UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
  172. flowLayout.minimumLineSpacing = 0;
  173. flowLayout.minimumInteritemSpacing = 0;
  174. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
  175. _collectionView.backgroundColor = BAKit_Color_Clear_pod;
  176. _collectionView.dataSource = self;
  177. _collectionView.delegate = self;
  178. _collectionView.scrollEnabled = NO;
  179. [self addSubview:_collectionView];
  180. }
  181. return _collectionView;
  182. }
  183. - (void)setGridViewType:(BAGridViewType)gridViewType
  184. {
  185. _gridViewType = gridViewType;
  186. if (self.gridViewType == BAGridViewTypeImageTitle)
  187. {
  188. [_collectionView registerClass:[BAGridCollectionCell class] forCellWithReuseIdentifier:kCellID];
  189. }
  190. else if (self.gridViewType == BAGridViewTypeTitleDesc)
  191. {
  192. [_collectionView registerClass:[BAGridViewTypeTitleDescCell class] forCellWithReuseIdentifier:kCellID2];
  193. }
  194. [self.collectionView reloadData];
  195. }
  196. - (void)setBa_gridView_rowCount:(NSInteger)ba_gridView_rowCount
  197. {
  198. _ba_gridView_rowCount = ba_gridView_rowCount;
  199. self.gridItem_w = (BAKit_SCREEN_WIDTH - (ba_gridView_rowCount - 1) * self.ba_gridView_lineWidth)/ba_gridView_rowCount;
  200. [self.collectionView reloadData];
  201. }
  202. - (void)setBa_gridView_lineWidth:(CGFloat)ba_gridView_lineWidth
  203. {
  204. _ba_gridView_lineWidth = ba_gridView_lineWidth;
  205. [self.collectionView reloadData];
  206. }
  207. - (void)setBa_gridView_itemHeight:(CGFloat)ba_gridView_itemHeight
  208. {
  209. _ba_gridView_itemHeight = ba_gridView_itemHeight;
  210. [self.collectionView reloadData];
  211. }
  212. - (void)setBa_gridView_titleColor:(UIColor *)ba_gridView_titleColor
  213. {
  214. _ba_gridView_titleColor = ba_gridView_titleColor;
  215. [self.collectionView reloadData];
  216. }
  217. - (void)setBa_gridView_titleDescColor:(UIColor *)ba_gridView_titleDescColor
  218. {
  219. _ba_gridView_titleDescColor = ba_gridView_titleDescColor;
  220. [self.collectionView reloadData];
  221. }
  222. - (void)setBa_gridView_lineColor:(UIColor *)ba_gridView_lineColor
  223. {
  224. _ba_gridView_lineColor = ba_gridView_lineColor;
  225. [self.collectionView reloadData];
  226. }
  227. - (void)setBa_gridView_itemImageInset:(CGFloat)ba_gridView_itemImageInset
  228. {
  229. _ba_gridView_itemImageInset = ba_gridView_itemImageInset;
  230. [self.collectionView reloadData];
  231. }
  232. - (void)setShowLineView:(BOOL)showLineView
  233. {
  234. _showLineView = showLineView;
  235. if (!showLineView)
  236. {
  237. self.ba_gridView_lineWidth = 0;
  238. [self.collectionView reloadData];
  239. }
  240. }
  241. - (void)setBa_gridView_titleFont:(UIFont *)ba_gridView_titleFont
  242. {
  243. _ba_gridView_titleFont = ba_gridView_titleFont;
  244. [self.collectionView reloadData];
  245. }
  246. - (void)setBa_gridView_titleDescFont:(UIFont *)ba_gridView_titleDescFont
  247. {
  248. _ba_gridView_titleDescFont = ba_gridView_titleDescFont;
  249. [self.collectionView reloadData];
  250. }
  251. - (void)setBa_gridView_backgroundColor:(UIColor *)ba_gridView_backgroundColor
  252. {
  253. _ba_gridView_backgroundColor = ba_gridView_backgroundColor;
  254. self.backgroundColor = ba_gridView_backgroundColor;
  255. [self.collectionView reloadData];
  256. }
  257. - (void)setBa_gridView_selectedBackgroundColor:(UIColor *)ba_gridView_selectedBackgroundColor
  258. {
  259. _ba_gridView_selectedBackgroundColor = ba_gridView_selectedBackgroundColor;
  260. }
  261. @end