BAGridView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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, weak) 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. UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
  73. flowLayout.minimumLineSpacing = 0;
  74. flowLayout.minimumInteritemSpacing = 0;
  75. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
  76. collectionView.backgroundColor = BAKit_Color_Clear_pod;
  77. collectionView.dataSource = self;
  78. collectionView.delegate = self;
  79. collectionView.scrollEnabled = NO;
  80. collectionView.hidden = NO;
  81. [self addSubview:collectionView];
  82. self.collectionView = collectionView;
  83. // 默认配置
  84. self.gridViewType = BAGridViewTypeImageTitle;
  85. self.ba_gridView_rowCount = 4;
  86. self.ba_gridView_lineColor = BAKit_Color_Gray_10_pod;
  87. self.ba_gridView_lineWidth = BAKit_Flat_pod(0.5f);
  88. self.ba_gridView_titleColor = BAKit_Color_Black_pod;
  89. self.ba_gridView_titleDescColor = BAKit_Color_Gray_9_pod;
  90. self.ba_gridView_itemImageInset = 0;
  91. self.ba_gridView_backgroundColor = BAKit_Color_White_pod;
  92. }
  93. - (void)layoutSubviews
  94. {
  95. [super layoutSubviews];
  96. self.collectionView.frame = self.bounds;
  97. }
  98. #pragma mark - UICollectionViewDelegate, UICollectionViewDataSource
  99. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  100. {
  101. return self.dataArray.count;
  102. }
  103. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  104. {
  105. BAGridCollectionCell *cell;
  106. BAGridViewTypeTitleDescCell *cell2;
  107. [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
  108. if (self.gridViewType == BAGridViewTypeImageTitle)
  109. {
  110. cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
  111. cell.backgroundColor = BAKit_Color_Clear_pod;
  112. cell.model = self.dataArray[indexPath.row];
  113. cell.ba_gridView_titleColor = self.ba_gridView_titleColor;
  114. cell.ba_gridView_lineColor = self.ba_gridView_lineColor;
  115. cell.ba_gridView_lineWidth = self.ba_gridView_lineWidth;
  116. cell.ba_gridView_itemImageInset = self.ba_gridView_itemImageInset;
  117. cell.ba_gridView_titleFont = self.ba_gridView_titleFont;
  118. return cell;
  119. }
  120. else if (self.gridViewType == BAGridViewTypeTitleDesc)
  121. {
  122. cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:kCellID2 forIndexPath:indexPath];
  123. cell2.backgroundColor = BAKit_Color_Clear_pod;
  124. cell2.model = self.dataArray[indexPath.row];
  125. cell2.ba_gridView_titleColor = self.ba_gridView_titleColor;
  126. cell2.ba_gridView_titleDescColor = self.ba_gridView_titleDescColor;
  127. cell2.ba_gridView_lineColor = self.ba_gridView_lineColor;
  128. cell2.ba_gridView_lineWidth = self.ba_gridView_lineWidth;
  129. cell2.ba_gridView_itemImageInset = self.ba_gridView_itemImageInset;
  130. cell2.ba_gridView_titleFont = self.ba_gridView_titleFont;
  131. cell2.ba_gridView_titleDescFont = self.ba_gridView_titleDescFont;
  132. return cell2;
  133. }
  134. return [UICollectionViewCell new];
  135. }
  136. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  137. {
  138. // [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  139. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  140. // 选中之后的cell变颜色
  141. [self ba_updateCell:cell indexPath:indexPath selected:YES];
  142. BAGridItemModel *model = self.dataArray[indexPath.row];
  143. if (self.ba_gridViewBlock)
  144. {
  145. self.ba_gridViewBlock(model, indexPath);
  146. }
  147. }
  148. // 取消选中操作
  149. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
  150. {
  151. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  152. [self ba_updateCell:cell indexPath:indexPath selected:NO];
  153. }
  154. // 改变cell的背景颜色
  155. - (void)ba_updateCell:(id)cell
  156. indexPath:(NSIndexPath *)indexPath
  157. selected:(BOOL)selected
  158. {
  159. [UIView animateWithDuration:0.20f animations:^{
  160. ((UICollectionViewCell *)cell).backgroundColor = selected ? self.ba_gridView_selectedBackgroundColor : self.ba_gridView_backgroundColor;
  161. } completion:^(BOOL finished) {
  162. [UIView animateWithDuration:0.08f animations:^{
  163. ((UICollectionViewCell *)cell).backgroundColor = self.backgroundColor;
  164. }];
  165. }];
  166. self.selectIndexPath = indexPath;
  167. }
  168. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  169. {
  170. return (CGSizeMake(self.gridItem_w, self.ba_gridView_itemHeight));
  171. }
  172. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  173. {
  174. return UIEdgeInsetsMake(0, 0, 0, 0);
  175. }
  176. #pragma mark - setter / getter
  177. - (UICollectionView *)collectionView
  178. {
  179. if (!_collectionView)
  180. {
  181. UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
  182. flowLayout.minimumLineSpacing = 0;
  183. flowLayout.minimumInteritemSpacing = 0;
  184. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
  185. _collectionView.backgroundColor = BAKit_Color_Clear_pod;
  186. _collectionView.dataSource = self;
  187. _collectionView.delegate = self;
  188. _collectionView.scrollEnabled = NO;
  189. [self addSubview:_collectionView];
  190. }
  191. return _collectionView;
  192. }
  193. - (void)setGridViewType:(BAGridViewType)gridViewType
  194. {
  195. _gridViewType = gridViewType;
  196. if (self.gridViewType == BAGridViewTypeImageTitle)
  197. {
  198. [_collectionView registerClass:[BAGridCollectionCell class] forCellWithReuseIdentifier:kCellID];
  199. }
  200. else if (self.gridViewType == BAGridViewTypeTitleDesc)
  201. {
  202. [_collectionView registerClass:[BAGridViewTypeTitleDescCell class] forCellWithReuseIdentifier:kCellID2];
  203. }
  204. [self.collectionView reloadData];
  205. }
  206. - (void)setBa_gridView_rowCount:(NSInteger)ba_gridView_rowCount
  207. {
  208. _ba_gridView_rowCount = ba_gridView_rowCount;
  209. self.gridItem_w = (BAKit_SCREEN_WIDTH - (ba_gridView_rowCount - 1) * self.ba_gridView_lineWidth)/ba_gridView_rowCount;
  210. [self.collectionView reloadData];
  211. }
  212. - (void)setBa_gridView_lineWidth:(CGFloat)ba_gridView_lineWidth
  213. {
  214. _ba_gridView_lineWidth = ba_gridView_lineWidth;
  215. [self.collectionView reloadData];
  216. }
  217. - (void)setBa_gridView_itemHeight:(CGFloat)ba_gridView_itemHeight
  218. {
  219. _ba_gridView_itemHeight = ba_gridView_itemHeight;
  220. [self.collectionView reloadData];
  221. }
  222. - (void)setBa_gridView_titleColor:(UIColor *)ba_gridView_titleColor
  223. {
  224. _ba_gridView_titleColor = ba_gridView_titleColor;
  225. [self.collectionView reloadData];
  226. }
  227. - (void)setBa_gridView_titleDescColor:(UIColor *)ba_gridView_titleDescColor
  228. {
  229. _ba_gridView_titleDescColor = ba_gridView_titleDescColor;
  230. [self.collectionView reloadData];
  231. }
  232. - (void)setBa_gridView_lineColor:(UIColor *)ba_gridView_lineColor
  233. {
  234. _ba_gridView_lineColor = ba_gridView_lineColor;
  235. [self.collectionView reloadData];
  236. }
  237. - (void)setBa_gridView_itemImageInset:(CGFloat)ba_gridView_itemImageInset
  238. {
  239. _ba_gridView_itemImageInset = ba_gridView_itemImageInset;
  240. [self.collectionView reloadData];
  241. }
  242. - (void)setShowLineView:(BOOL)showLineView
  243. {
  244. _showLineView = showLineView;
  245. if (!showLineView)
  246. {
  247. self.ba_gridView_lineWidth = 0;
  248. [self.collectionView reloadData];
  249. }
  250. }
  251. - (void)setBa_gridView_titleFont:(UIFont *)ba_gridView_titleFont
  252. {
  253. _ba_gridView_titleFont = ba_gridView_titleFont;
  254. [self.collectionView reloadData];
  255. }
  256. - (void)setBa_gridView_titleDescFont:(UIFont *)ba_gridView_titleDescFont
  257. {
  258. _ba_gridView_titleDescFont = ba_gridView_titleDescFont;
  259. [self.collectionView reloadData];
  260. }
  261. - (void)setBa_gridView_backgroundColor:(UIColor *)ba_gridView_backgroundColor
  262. {
  263. _ba_gridView_backgroundColor = ba_gridView_backgroundColor;
  264. self.backgroundColor = ba_gridView_backgroundColor;
  265. [self.collectionView reloadData];
  266. }
  267. - (void)setBa_gridView_selectedBackgroundColor:(UIColor *)ba_gridView_selectedBackgroundColor
  268. {
  269. _ba_gridView_selectedBackgroundColor = ba_gridView_selectedBackgroundColor;
  270. }
  271. @end