ItemListView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //
  2. // NewUtilityView.m
  3. // zhuxun
  4. //
  5. // Created by winsoft on 17/4/1.
  6. //
  7. //
  8. #import "ItemListView.h"
  9. #import "CustomButton.h"
  10. #define UtilisIconWidth 50
  11. #define UtilisIconHeight 72
  12. #define UtilityCellWidth self.frame.size.width
  13. #define UtilityCellHeight ItemListViewH
  14. #define MaxUtilisCountOnePage 8
  15. #define LineNum 4
  16. #define RowNum 2
  17. #define UtilityCellIdentifier @"UtilityCellIdentifier"
  18. @interface ItemListView()<UICollectionViewDataSource, UICollectionViewDelegate>
  19. @property (nonatomic , weak) UICollectionView *mainCollectionView;
  20. @property (nonatomic , strong) NSMutableArray *utilityIconPagesArray;//总的utiilitys
  21. @property (nonatomic , strong) NSMutableArray *utilityTitlePagesArray;
  22. @property (nonatomic , strong) NSArray *itemTypeArray;
  23. @property (nonatomic , assign) NSInteger utilityPagesCount;//总的utiility count
  24. @property (nonatomic, assign)NSInteger currentPageIndex;
  25. @end
  26. @implementation ItemListView
  27. - (NSMutableArray *)utilityIconPagesArray
  28. {
  29. if (!_utilityIconPagesArray) {
  30. _utilityIconPagesArray = [NSMutableArray array];
  31. }
  32. return _utilityIconPagesArray;
  33. }
  34. - (NSMutableArray *)utilityTitlePagesArray
  35. {
  36. if (!_utilityTitlePagesArray) {
  37. _utilityTitlePagesArray = [NSMutableArray array];
  38. }
  39. return _utilityTitlePagesArray;
  40. }
  41. //-(instancetype)initWithItemList:(NSArray *)array{
  42. //
  43. // if (self =[self init]) {
  44. // if (!array) {
  45. // self.itemTypeArray = array;
  46. //
  47. // }
  48. // else{
  49. // //未传入则不填充;
  50. // }
  51. // }
  52. // return self;
  53. //}
  54. - (instancetype)init
  55. {
  56. if (self = [super init]) {
  57. [self.layer setBorderWidth:0.5];
  58. [self.layer setBorderColor:RGB(188, 188, 188).CGColor];
  59. self.backgroundColor = [UIColor whiteColor];
  60. /*
  61. ItemType_Developing = 0,
  62. ItemType_SyncContact = 1,
  63. ItemType_Recycle = 2,
  64. ItemType_Theme = 3
  65. 传进来的数组字典 应该为 @{[
  66. @imgname:@"itemtype_SyncContact",
  67. @"btntitle":@"通讯录同步",
  68. @"ItemType":@(ItemType_SyncContact)],
  69. @imgname:@"itemtype_SyncContact",
  70. @"btntitle":@"通讯录同步",
  71. @"ItemType":@(ItemType_SyncContact)]
  72. ,...以此类推
  73. }
  74. */
  75. NSArray *iconArray = @[@"itemtype_SyncContact",@"itemType_Recycle",@"itemType_Theme",@"itemType_Developing"];
  76. NSArray *titleArray = @[@"通讯录",@"照片",@"文档",@"视频",@"音频"];
  77. //self.itemTypeArray = @[@(CSFileTypeAll),@(CSFileTypePic),@(CSFileTypeDoc),@(CSFileTypeVideo),@(CSFileTypeAudio)];
  78. //实际多少页
  79. NSInteger pageCount = (_itemTypeArray.count / (LineNum * RowNum));
  80. if (_itemTypeArray.count % (LineNum * RowNum) != 0) {
  81. pageCount += 1;
  82. }
  83. self.utilityPagesCount = pageCount;
  84. for (int i = 0; i< self.utilityPagesCount; i ++) {
  85. NSInteger loc = i * (LineNum * RowNum );
  86. NSInteger len = 0;
  87. //最大值.
  88. if (i < self.utilityPagesCount - 1) {
  89. len = (LineNum * RowNum);
  90. }else len = iconArray.count - i*(LineNum * RowNum);
  91. NSArray *IconUtitlityArray = [iconArray subarrayWithRange:NSMakeRange(loc, len)];
  92. NSArray *TitleUtitlityArray = [titleArray subarrayWithRange:NSMakeRange(loc, len)];
  93. [self.utilityIconPagesArray addObject:IconUtitlityArray];
  94. [self.utilityTitlePagesArray addObject:TitleUtitlityArray];
  95. }
  96. CGFloat itemsW = UtilityCellWidth;
  97. //(deviceWidth - (collectionViewItmesInRow + 1)*mainViewMargin)/3.0f;
  98. CGFloat itemsH = UtilityCellHeight;
  99. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  100. flowLayout.itemSize = CGSizeMake(itemsW, itemsH);
  101. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  102. UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];
  103. collectionView.backgroundColor = [UIColor whiteColor];
  104. collectionView.pagingEnabled = YES;
  105. //能否和tableview一样设置headerview的效果..
  106. // collectionView.backgroundColor = [UIColor clearColor];
  107. collectionView.showsVerticalScrollIndicator = NO;
  108. collectionView.showsHorizontalScrollIndicator = NO;
  109. [collectionView registerClass:[PageUtilityCell class] forCellWithReuseIdentifier:UtilityCellIdentifier];
  110. collectionView.dataSource = self;
  111. collectionView.delegate = self;
  112. [self addSubview:collectionView];
  113. self.mainCollectionView = collectionView;
  114. }
  115. return self;
  116. }
  117. - (void)reloadData
  118. {
  119. [self.mainCollectionView reloadData];
  120. }
  121. #pragma mark --
  122. #pragma mrak -- collectionviewdatasource
  123. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  124. {
  125. return self.utilityIconPagesArray.count;
  126. }
  127. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  128. {
  129. PageUtilityCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:UtilityCellIdentifier forIndexPath:indexPath];
  130. // cell.backgroundColor = [UIColor redColor];
  131. if (cell == nil) {
  132. NSLog(@"生成cell失败");
  133. }
  134. __weak typeof(self)weakself = self;
  135. cell.utilityOperateBlock = ^(ItemType type)
  136. {
  137. if (weakself.utilityOperateBlock && weakself.currentPageIndex * (LineNum * RowNum - 1) + type < weakself.itemTypeArray.count) {
  138. NSNumber *typeNum = weakself.itemTypeArray[weakself.currentPageIndex * (LineNum * RowNum - 1) + type];
  139. weakself.utilityOperateBlock((ItemType)typeNum.integerValue);
  140. }
  141. };
  142. [cell setUitilityIconsArray:self.utilityIconPagesArray[indexPath.row] titlesArray:self.utilityTitlePagesArray[indexPath.row]];
  143. return cell;
  144. }
  145. #pragma mark -
  146. #pragma mark - UIActivityItemSource
  147. #pragma mark -
  148. #pragma mark - uicollectionviewlayoutdatasource
  149. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  150. {
  151. return CGSizeMake(UtilityCellWidth ,UtilityCellHeight);
  152. }
  153. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  154. {
  155. return UIEdgeInsetsMake(0, 0, 0 ,0);
  156. }
  157. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  158. return 0;
  159. }
  160. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  161. {
  162. return 0;
  163. }
  164. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  165. {
  166. self.currentPageIndex = collectionView.contentOffset.x / collectionView.self.width;
  167. }
  168. #pragma mrak --
  169. - (void)layoutSubviews
  170. {
  171. [super layoutSubviews];
  172. self.mainCollectionView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
  173. }
  174. @end
  175. @interface PageUtilityCell()
  176. @property (nonatomic , weak)PageUtilityView *pageUtilityView;
  177. @end
  178. @implementation PageUtilityCell
  179. - (instancetype)initWithFrame:(CGRect)frame
  180. {
  181. if (self = [super initWithFrame:frame]) {
  182. PageUtilityView *pageUtilityView = [[PageUtilityView alloc]init];
  183. __weak typeof(self)weakself = self;
  184. pageUtilityView.utilityOperateBlock = ^(ItemType itemtype){
  185. if (weakself.utilityOperateBlock) {
  186. weakself.utilityOperateBlock(itemtype);
  187. }
  188. };
  189. [self.contentView addSubview:pageUtilityView];
  190. self.pageUtilityView = pageUtilityView;
  191. }
  192. return self;
  193. }
  194. - (void)setUitilityIconsArray:(NSDictionary *)itemListDic{
  195. NSArray *array;
  196. }
  197. - (void)setUitilityIconsArray:(NSArray *)iconArray titlesArray:(NSArray *)titleArray
  198. {
  199. [self.pageUtilityView setUitilityIconsArray:iconArray titlesArray:titleArray];
  200. }
  201. - (void)layoutSubviews
  202. {
  203. [super layoutSubviews];
  204. self.pageUtilityView.frame = self.bounds;
  205. }
  206. @end
  207. @interface PageUtilityView()
  208. @end
  209. @implementation PageUtilityView
  210. - (instancetype)init
  211. {
  212. if(self = [super init])
  213. {
  214. for (int i = 0 ; i < RowNum; i ++) {
  215. for (int j = 0 ; j < LineNum; j++) {
  216. CustomButton *customBtn = [[CustomButton alloc]initCustomBtnWithImageDirection:ImagePostionTop imageViewSize:CGSizeMake(UtilisIconWidth, UtilisIconWidth) imageLabelMargin:5 titleLabelFont:[UIFont systemFontOfSize:14] imageIcon:@"utility_image" selectIcon:nil title:@"更多" btnWidth:UtilisIconWidth];
  217. [customBtn setTitleColor:RGB(165, 165, 165) forState:UIControlStateNormal];
  218. customBtn.tag = i * LineNum + j;
  219. [self addSubview:customBtn];
  220. [customBtn addTarget:self action:@selector(utilityAction:) forControlEvents:UIControlEventTouchUpInside];
  221. }
  222. }
  223. }
  224. return self;
  225. }
  226. - (void)utilityAction:(CustomButton *)btn
  227. {
  228. if (self.utilityOperateBlock) {
  229. self.utilityOperateBlock((ItemType)btn.tag);
  230. }
  231. }
  232. - (void)setUitilityIconsArray:(NSArray *)iconArray titlesArray:(NSArray *)titleArray
  233. {
  234. for (CustomButton *customBtn in self.subviews) {
  235. if ([customBtn isKindOfClass:[CustomButton class]]) {
  236. if (customBtn.tag > iconArray.count - 1 || customBtn.tag > titleArray.count - 1) {
  237. [customBtn setImage:nil forState:UIControlStateNormal];;
  238. [customBtn setTitle:nil forState:UIControlStateNormal];
  239. }else{
  240. [customBtn setImage:[UIImage imageNamed:iconArray[customBtn.tag]] forState:UIControlStateNormal];
  241. [customBtn setTitle:titleArray[customBtn.tag] forState:UIControlStateNormal];
  242. }
  243. }
  244. }
  245. }
  246. - (void)layoutSubviews
  247. {
  248. [super layoutSubviews];
  249. for (CustomButton *customBtn in self.subviews) {
  250. if ([customBtn isKindOfClass:[CustomButton class]]) {
  251. CGFloat btnMarginX = (self.size.width - (LineNum * UtilisIconWidth)) / (LineNum + 1);
  252. CGFloat btnMarginY = 10;
  253. CGFloat btnX = (customBtn.tag % LineNum) * (UtilisIconWidth + btnMarginX) + btnMarginX;
  254. CGFloat btnY = (customBtn.tag / LineNum) * (customBtn.customBtnHeight + btnMarginY) + btnMarginY;
  255. CGFloat btnW = UtilisIconWidth;
  256. CGFloat btnH = customBtn.customBtnHeight;
  257. customBtn.frame = CGRectMake(btnX, btnY, btnW, btnH);
  258. }
  259. }
  260. }
  261. @end