123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- //
- // DemoCustomView2.m
- // Demo
- //
- // Created by 路 on 2019/2/20.
- // Copyright © 2019年 路. All rights reserved.
- //
- #import "JLSelectView.h"
- #define WIDTH (self.bounds.size.width)*1/4
- #import "NodeCollectionView.h"
- #import "NodeCollectionViewCell.h"
- #import "JLSubCollectionView.h"
- #import "JLSelectModel.h"
- #import "MJExtension.h"
- @interface JLSelectView()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate,JLSubCollectionDelegate>{
- JLSubCollectionView *_cv;
- }
- @property(nonatomic, strong) NodeCollectionView *nodeCV;
- @property(nonatomic, strong) NSMutableArray<JLSelectModel *> *dataSource;
- @property(nonatomic, assign) NSInteger currentIndex;
- @property(nonatomic, strong) UIScrollView *contentSV;
- @property(nonatomic, strong) NSMutableArray<JLSubCollectionView *> *viewData;
- @property(nonatomic, strong) NSArray *stateArr;//标记分类
- @property(nonatomic, strong) NSMutableArray *dataSource2;
- @end
- @implementation JLSelectView
- - (id)init {
- self = [super init];
- if (self) {
- self.backgroundColor = [UIColor whiteColor];
- self.layer.cornerRadius = 10;
- self.clipsToBounds = YES;
- /// _dataSource2 = [NSMutableArray arrayWithObjects:@"官方彩",@"传统彩", nil];
-
- } return self;
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
- [self creatUI];
- }
- - (void)setDataArray:(NSArray *)dataArray {
- _dataArray = dataArray;
- _dataSource = [NSMutableArray array];
- for (NSDictionary *dict in dataArray) {
- [JLSelectModel mj_setupObjectClassInArray:^NSDictionary *{
- return @{
- @"data" : @"JLSelectItemModel"
- // @"statuses" : [Status class],
- };
- }];
- JLSelectModel *model = [JLSelectModel mj_objectWithKeyValues:dict];
- [self.dataSource addObject:model];
- }
- }
- - (void)creatUI {
- self.backgroundColor = [UIColor whiteColor];
- self.viewData = [NSMutableArray arrayWithCapacity:0];
- // self.dataSource = @[@"官方",@"传统"];
- //分别标记上面的分类
- self.stateArr = @[@"0",@"10"];
- [self addSubview:self.nodeCV];
-
-
- self.contentSV = [UIScrollView new];
- self.contentSV.frame = CGRectMake(0, 70, self.bounds.size.width, self.bounds.size.height);
- //self.contentSV.backgroundColor=[UIColor greenColor];
- self.contentSV.delegate = self;
- [self addSubview:self.contentSV];
- self.contentSV.pagingEnabled = YES;
- self.contentSV.showsHorizontalScrollIndicator = NO;
- self.contentSV.bounces = NO;
- for (int i=0; i<self.dataSource.count; i++) {
- // 创建子View
- JLSelectModel *model = _dataSource[i];
- _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];
- if (i==0) {
- _cv.nineAndTenStr=10;
- }
- _cv.model = model;
- _cv.tag=i+1000;
- _cv.delegate = self;
- [self.contentSV addSubview:_cv];
- if (i==0) {
- // [_cv.tv.mj_header beginRefreshing];
- }
- [self.viewData addObject:_cv];
- }
- self.contentSV.contentSize = CGSizeMake(self.dataSource.count*self.bounds.size.width, self.contentSV.frame.size.height);
- UIView *line=[[UIView alloc] initWithFrame:CGRectMake(40, CGRectGetMaxY(_nodeCV.frame)+5, self.bounds.size.width-80, 0.5)];
- line.backgroundColor = [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:0.5];
- [self addSubview:line];
- }
- -(NodeCollectionView *)nodeCV{
- if(!_nodeCV){
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
- layout.sectionInset = UIEdgeInsetsMake(0, WIDTH-10, 0, 0);
- layout.minimumLineSpacing = 0;
- layout.minimumInteritemSpacing = 0;
- layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- _nodeCV = [[NodeCollectionView alloc]initWithFrame:CGRectMake(0, 20, self.bounds.size.width, 50) collectionViewLayout:layout];
- _nodeCV.backgroundColor = [UIColor whiteColor];
- _nodeCV.showsHorizontalScrollIndicator = NO;
- _nodeCV.bounces = NO;
- _nodeCV.delegate = self;
- _nodeCV.dataSource = self;
- [_nodeCV registerClass:[NodeCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([NodeCollectionViewCell class])];
- }
- return _nodeCV;
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
- return _dataSource.count;
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- return CGSizeMake(WIDTH, 50);
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
- JLSelectModel *model = _dataSource[indexPath.item];
- NodeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([NodeCollectionViewCell class]) forIndexPath:indexPath];
- for (UIView *subView in cell.contentView.subviews) {
- [subView removeFromSuperview];
- }
- UILabel *lab = [UILabel new];
- lab.backgroundColor = [UIColor whiteColor];
- lab.text =[NSString stringWithFormat:@"%@", model.name];
- lab.font = [UIFont systemFontOfSize:17];
- lab.textColor = [UIColor blackColor];
- lab.textAlignment = NSTextAlignmentCenter;
- lab.frame = CGRectMake(0, 15, WIDTH, 20);
- [cell.contentView addSubview:lab];
- if (_currentIndex==indexPath.item) {
- lab.textColor = [UIColor redColor];
- UIView *line=[[UIView alloc] initWithFrame:CGRectMake(CGRectGetMidX(lab.frame)-25, CGRectGetMaxY(lab.frame)+10, 50, 2)];
- line.backgroundColor=[UIColor redColor];
- [cell.contentView addSubview:line];
- }
- return cell;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
- [collectionView deselectItemAtIndexPath:indexPath animated:YES];
- if(indexPath.item==_currentIndex)return;
- _currentIndex = indexPath.item;
- NSIndexPath *indexpath = [NSIndexPath indexPathForRow:_currentIndex inSection:0];
- [self.nodeCV reloadData];
- [self.nodeCV scrollToItemAtIndexPath:indexpath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
- [self.contentSV setContentOffset:CGPointMake(_currentIndex*self.bounds.size.width, 0) animated:NO];
- //JLSubCollectionView *cv = self.viewData[_currentIndex];
- // [cv.tv.mj_header beginRefreshing];
- }
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
- if([scrollView isKindOfClass:[NodeCollectionView class]]){
- return;
- }
- NSInteger index = scrollView.contentOffset.x/self.bounds.size.width;
- if(_currentIndex==index)return;
- _currentIndex = index;
- JLSubCollectionView *cv = self.viewData[_currentIndex];
- // [cv.tv.mj_header beginRefreshing];
- [self.nodeCV reloadData];
- NSIndexPath *indexpath = [NSIndexPath indexPathForRow:_currentIndex inSection:0];
- [self.nodeCV scrollToItemAtIndexPath:indexpath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
- }
- // MARK: JLSubCollectionDelegate
- - (void)subCollection:(JLSubCollectionView *)subCollectionView didSelectedItemindexPath:(NSIndexPath *)indexPath model:(JLSelectModel *)model {
- if (_returnModelBlock) {
- _returnModelBlock(model,indexPath);
- }
- }
- - (void)setReturnModelBlock:(ReturnModelBlock)returnModelBlock {
- _returnModelBlock = returnModelBlock;
- }
- // 准备弹出(初始化弹层位置)
- - (void)willPopupContainer:(DSHPopupContainer *)container; {
- CGRect frame = self.frame;
- frame.size = CGSizeMake(300, 480);
- frame.origin.x = (container.frame.size.width - frame.size.width) * .5;
- frame.origin.y = (container.frame.size.height - frame.size.height) * .5;
- self.frame = frame;
- }
- // 已弹出(做弹出动画)
- - (void)didPopupContainer:(DSHPopupContainer *)container duration:(NSTimeInterval)duration; {
- self.transform = CGAffineTransformMakeScale(1.1, 1.1);
- [UIView animateWithDuration:duration animations:^{
- self.transform = CGAffineTransformMakeScale(1.f, 1.f);
- }];
- }
- // 将要移除(做移除动画)
- - (void)willDismissContainer:(DSHPopupContainer *)container duration:(NSTimeInterval)duration; {
- CGRect frame = self.frame;
- frame.origin.y = container.frame.size.height;
- [UIView animateWithDuration:duration animations:^{
- self.alpha = 0.f;
- }];
- }
- @end
|