JLSelectView.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // DemoCustomView2.m
  3. // Demo
  4. //
  5. // Created by 路 on 2019/2/20.
  6. // Copyright © 2019年 路. All rights reserved.
  7. //
  8. #import "JLSelectView.h"
  9. #define WIDTH (self.bounds.size.width)*1/4
  10. #import "NodeCollectionView.h"
  11. #import "NodeCollectionViewCell.h"
  12. #import "JLSubCollectionView.h"
  13. #import "JLSelectModel.h"
  14. #import "MJExtension.h"
  15. @interface JLSelectView()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate,JLSubCollectionDelegate>{
  16. JLSubCollectionView *_cv;
  17. }
  18. @property(nonatomic, strong) NodeCollectionView *nodeCV;
  19. @property(nonatomic, strong) NSMutableArray<JLSelectModel *> *dataSource;
  20. @property(nonatomic, assign) NSInteger currentIndex;
  21. @property(nonatomic, strong) UIScrollView *contentSV;
  22. @property(nonatomic, strong) NSMutableArray<JLSubCollectionView *> *viewData;
  23. @property(nonatomic, strong) NSArray *stateArr;//标记分类
  24. @property(nonatomic, strong) NSMutableArray *dataSource2;
  25. @end
  26. @implementation JLSelectView
  27. - (id)init {
  28. self = [super init];
  29. if (self) {
  30. self.backgroundColor = [UIColor whiteColor];
  31. self.layer.cornerRadius = 10;
  32. self.clipsToBounds = YES;
  33. /// _dataSource2 = [NSMutableArray arrayWithObjects:@"官方彩",@"传统彩", nil];
  34. } return self;
  35. }
  36. - (void)layoutSubviews {
  37. [super layoutSubviews];
  38. [self creatUI];
  39. }
  40. - (void)setDataArray:(NSArray *)dataArray {
  41. _dataArray = dataArray;
  42. _dataSource = [NSMutableArray array];
  43. for (NSDictionary *dict in dataArray) {
  44. [JLSelectModel mj_setupObjectClassInArray:^NSDictionary *{
  45. return @{
  46. @"data" : @"JLSelectItemModel"
  47. // @"statuses" : [Status class],
  48. };
  49. }];
  50. JLSelectModel *model = [JLSelectModel mj_objectWithKeyValues:dict];
  51. [self.dataSource addObject:model];
  52. }
  53. }
  54. - (void)creatUI {
  55. self.backgroundColor = [UIColor whiteColor];
  56. self.viewData = [NSMutableArray arrayWithCapacity:0];
  57. // self.dataSource = @[@"官方",@"传统"];
  58. //分别标记上面的分类
  59. self.stateArr = @[@"0",@"10"];
  60. [self addSubview:self.nodeCV];
  61. self.contentSV = [UIScrollView new];
  62. self.contentSV.frame = CGRectMake(0, 70, self.bounds.size.width, self.bounds.size.height);
  63. //self.contentSV.backgroundColor=[UIColor greenColor];
  64. self.contentSV.delegate = self;
  65. [self addSubview:self.contentSV];
  66. self.contentSV.pagingEnabled = YES;
  67. self.contentSV.showsHorizontalScrollIndicator = NO;
  68. self.contentSV.bounces = NO;
  69. for (int i=0; i<self.dataSource.count; i++) {
  70. // 创建子View
  71. JLSelectModel *model = _dataSource[i];
  72. _cv=[[JLSubCollectionView alloc] initWithFrame:CGRectMake(self.bounds.size.width*i, 0, self.bounds.size.width, self.contentSV.frame.size.height) contentType:model.name selfVc:self];
  73. if (i==0) {
  74. _cv.nineAndTenStr=10;
  75. }
  76. _cv.model = model;
  77. _cv.tag=i+1000;
  78. _cv.delegate = self;
  79. [self.contentSV addSubview:_cv];
  80. if (i==0) {
  81. // [_cv.tv.mj_header beginRefreshing];
  82. }
  83. [self.viewData addObject:_cv];
  84. }
  85. self.contentSV.contentSize = CGSizeMake(self.dataSource.count*self.bounds.size.width, self.contentSV.frame.size.height);
  86. UIView *line=[[UIView alloc] initWithFrame:CGRectMake(40, CGRectGetMaxY(_nodeCV.frame)+5, self.bounds.size.width-80, 0.5)];
  87. line.backgroundColor = [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:0.5];
  88. [self addSubview:line];
  89. }
  90. -(NodeCollectionView *)nodeCV{
  91. if(!_nodeCV){
  92. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
  93. layout.sectionInset = UIEdgeInsetsMake(0, WIDTH-10, 0, 0);
  94. layout.minimumLineSpacing = 0;
  95. layout.minimumInteritemSpacing = 0;
  96. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  97. _nodeCV = [[NodeCollectionView alloc]initWithFrame:CGRectMake(0, 20, self.bounds.size.width, 50) collectionViewLayout:layout];
  98. _nodeCV.backgroundColor = [UIColor whiteColor];
  99. _nodeCV.showsHorizontalScrollIndicator = NO;
  100. _nodeCV.bounces = NO;
  101. _nodeCV.delegate = self;
  102. _nodeCV.dataSource = self;
  103. [_nodeCV registerClass:[NodeCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([NodeCollectionViewCell class])];
  104. }
  105. return _nodeCV;
  106. }
  107. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  108. return _dataSource.count;
  109. }
  110. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  111. return CGSizeMake(WIDTH, 50);
  112. }
  113. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  114. JLSelectModel *model = _dataSource[indexPath.item];
  115. NodeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([NodeCollectionViewCell class]) forIndexPath:indexPath];
  116. for (UIView *subView in cell.contentView.subviews) {
  117. [subView removeFromSuperview];
  118. }
  119. UILabel *lab = [UILabel new];
  120. lab.backgroundColor = [UIColor whiteColor];
  121. lab.text =[NSString stringWithFormat:@"%@", model.name];
  122. lab.font = [UIFont systemFontOfSize:17];
  123. lab.textColor = [UIColor blackColor];
  124. lab.textAlignment = NSTextAlignmentCenter;
  125. lab.frame = CGRectMake(0, 15, WIDTH, 20);
  126. [cell.contentView addSubview:lab];
  127. if (_currentIndex==indexPath.item) {
  128. lab.textColor = [UIColor redColor];
  129. UIView *line=[[UIView alloc] initWithFrame:CGRectMake(CGRectGetMidX(lab.frame)-25, CGRectGetMaxY(lab.frame)+10, 50, 2)];
  130. line.backgroundColor=[UIColor redColor];
  131. [cell.contentView addSubview:line];
  132. }
  133. return cell;
  134. }
  135. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  136. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  137. if(indexPath.item==_currentIndex)return;
  138. _currentIndex = indexPath.item;
  139. NSIndexPath *indexpath = [NSIndexPath indexPathForRow:_currentIndex inSection:0];
  140. [self.nodeCV reloadData];
  141. [self.nodeCV scrollToItemAtIndexPath:indexpath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
  142. [self.contentSV setContentOffset:CGPointMake(_currentIndex*self.bounds.size.width, 0) animated:NO];
  143. //JLSubCollectionView *cv = self.viewData[_currentIndex];
  144. // [cv.tv.mj_header beginRefreshing];
  145. }
  146. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  147. if([scrollView isKindOfClass:[NodeCollectionView class]]){
  148. return;
  149. }
  150. NSInteger index = scrollView.contentOffset.x/self.bounds.size.width;
  151. if(_currentIndex==index)return;
  152. _currentIndex = index;
  153. JLSubCollectionView *cv = self.viewData[_currentIndex];
  154. // [cv.tv.mj_header beginRefreshing];
  155. [self.nodeCV reloadData];
  156. NSIndexPath *indexpath = [NSIndexPath indexPathForRow:_currentIndex inSection:0];
  157. [self.nodeCV scrollToItemAtIndexPath:indexpath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
  158. }
  159. // MARK: JLSubCollectionDelegate
  160. - (void)subCollection:(JLSubCollectionView *)subCollectionView didSelectedItemindexPath:(NSIndexPath *)indexPath model:(JLSelectModel *)model {
  161. if (_returnModelBlock) {
  162. _returnModelBlock(model,indexPath);
  163. }
  164. }
  165. - (void)setReturnModelBlock:(ReturnModelBlock)returnModelBlock {
  166. _returnModelBlock = returnModelBlock;
  167. }
  168. // 准备弹出(初始化弹层位置)
  169. - (void)willPopupContainer:(DSHPopupContainer *)container; {
  170. CGRect frame = self.frame;
  171. frame.size = CGSizeMake(300, 480);
  172. frame.origin.x = (container.frame.size.width - frame.size.width) * .5;
  173. frame.origin.y = (container.frame.size.height - frame.size.height) * .5;
  174. self.frame = frame;
  175. }
  176. // 已弹出(做弹出动画)
  177. - (void)didPopupContainer:(DSHPopupContainer *)container duration:(NSTimeInterval)duration; {
  178. self.transform = CGAffineTransformMakeScale(1.1, 1.1);
  179. [UIView animateWithDuration:duration animations:^{
  180. self.transform = CGAffineTransformMakeScale(1.f, 1.f);
  181. }];
  182. }
  183. // 将要移除(做移除动画)
  184. - (void)willDismissContainer:(DSHPopupContainer *)container duration:(NSTimeInterval)duration; {
  185. CGRect frame = self.frame;
  186. frame.origin.y = container.frame.size.height;
  187. [UIView animateWithDuration:duration animations:^{
  188. self.alpha = 0.f;
  189. }];
  190. }
  191. @end