iOS 选项卡

iOS 选项卡_第1张图片
视图控制器选项卡.gif
//
//  LYChildMenu.h
//  LYChildMenu
//
//  Created by HENAN on 17/1/10.
//  Copyright © 2017年 LYS. All rights reserved.
//

#import 

@class LYChildMenu;
@protocol LYChildMenuDataSource 
- (NSInteger)numberOfColumnsInMenu:(LYChildMenu *)menu;
- (NSString *)menu:(LYChildMenu *)menu titleForColumn:(NSInteger)column;
- (UIView *)menu:(LYChildMenu *)menu viewForColumn:(NSInteger)column;
@end

@interface LYChildMenu : UIView

@property (nonatomic,assign)id dataSource;

- (void)reloadData;
@end

@interface LYCollectionViewCell : UICollectionViewCell
@property (nonatomic,strong)UILabel *titleLabel;
@end

//
//  LYChildMenu.m
//  LYChildMenu
//
//  Created by HENAN on 17/1/10.
//  Copyright © 2017年 LYS. All rights reserved.
//

#import "LYChildMenu.h"

@interface LYChildMenu ()

@end
static NSString *identifier = @"LYCollectionViewCell";
@implementation LYChildMenu
{
    UICollectionViewFlowLayout *_flowLayout;
    UICollectionView *_collectionView;
    NSInteger _column;
    NSMutableArray *_titleAry;
    NSMutableArray *_viewAry;
    
    CGFloat _maxColumn;
    CGFloat _minColumnWidth;
    
    LYCollectionViewCell *_defaultSelectCell;
   
}
// 重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}
- (void)reloadData{
    [self customArguments];
    [self customSubViews];
}
// 初始化参数
- (void)customArguments{
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(numberOfColumnsInMenu:)]) {
        _column = [self.dataSource numberOfColumnsInMenu:self];
        if (_column > 4) {
            _maxColumn = 4;
        }else {
            _maxColumn = _column;
        }
        _minColumnWidth = CGRectGetWidth(self.bounds) / _maxColumn;
    }
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(menu:titleForColumn:)]) {
        _titleAry = [NSMutableArray array];
        for (int i = 0; i < _column; i++) {
            [_titleAry addObject:[self.dataSource menu:self titleForColumn:i]];
        }
    }
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(menu:viewForColumn:)]) {
        _viewAry = [NSMutableArray array];
        for (int i = 0; i < _column; i++) {
            [_viewAry addObject:[self.dataSource menu:self viewForColumn:i]];
        }
    }
}
// 自定义子视图
- (void)customSubViews{
    _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.minimumLineSpacing = 0;
    _flowLayout.minimumInteritemSpacing = 0;
    _flowLayout.itemSize = CGSizeMake(_minColumnWidth, 35);
    _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), 35) collectionViewLayout:_flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerClass:[LYCollectionViewCell class] forCellWithReuseIdentifier:identifier];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [self addSubview:_collectionView];
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, _collectionView.frame.origin.y + CGRectGetHeight(_collectionView.frame), CGRectGetWidth(self.bounds), 0.5)];
    line.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:line];
}
#pragma mark - UICollectionView的代理方法 -
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _column;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LYCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    if (indexPath.item == 0) {
        _defaultSelectCell = cell;
        _defaultSelectCell.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.4];
        CGRect frame = CGRectMake(0, _collectionView.frame.origin.y + _collectionView.frame.size.height + 1, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) - (_collectionView.frame.origin.y + _collectionView.frame.size.height + 1));
        UIView *view = [_viewAry objectAtIndex:0];
        view.frame = frame;
        [self addSubview:view];
    }
    cell.titleLabel.text = [_titleAry objectAtIndex:indexPath.item];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    _defaultSelectCell.backgroundColor = [UIColor whiteColor];
    LYCollectionViewCell *cell = (LYCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    _defaultSelectCell = cell;
    cell.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.4];
    [self selectAction:indexPath.item];
    [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:(UICollectionViewScrollPositionCenteredHorizontally) animated:YES];
}
#pragma mark - 点击事件触发 -
- (void)selectAction:(NSInteger)index{
    CGRect frame = CGRectMake(0, _collectionView.frame.origin.y + _collectionView.frame.size.height + 1, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) - (_collectionView.frame.origin.y + _collectionView.frame.size.height + 1));
    UIView *view = [_viewAry objectAtIndex:index];
    view.frame = frame;
    [self addSubview:view];
}
@end

@implementation LYCollectionViewCell

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        self.titleLabel = [[UILabel alloc] initWithFrame:self.contentView.bounds];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:12];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_titleLabel];
    }
    return _titleLabel;
}

@end




你可能感兴趣的:(iOS 选项卡)