ItemListView.m 12 KB

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