LXHSlideView

https://github.com/Tonyleu/DemoSlideView

#import

NS_ASSUME_NONNULL_BEGIN

@interface LXHSlideView : UIView

@property (nonatomic,strong)NSArray * vcArray;

- (instancetype)initWithFrame:(CGRect)frameWithViewControllers:(NSArray*)viewControllers;

@end






#import "LXHSlideView.h"

#define WindowWidth                        ([[UIScreen mainScreen] bounds].size.width)

#define WindowHeight                      ([[UIScreen mainScreen] bounds].size.height)

#define TopViewHight50

#define IndicatorWidth20

#define IndicatorHeight3

#define IndicatorCenter self.frame.size.width/((self.vcArray.count +1) *2) -10

#define HexUIColorFromRGB(rgbValue, a) [UIColor colorWithRed:((float)((rgbValue &0xFF0000) >>16))/255.0green:((float)((rgbValue &0xFF00) >>8))/255.0blue:((float)(rgbValue &0xFF))/255.0alpha:(a)]

@interface LXHSlideView()<UIScrollViewDelegate>

@property (nonatomic,strong) UIView *topView;

@property (nonatomic,strong) UIView *slideView;

@property (nonatomic,strong) UIScrollView *topScrollView;

@property (nonatomic,strong) UIScrollView *bottomScrollView;

@property (nonatomic,strong) NSMutableArray *btnArray;

@end

@implementation LXHSlideView

- (instancetype)initWithFrame:(CGRect)frameWithViewControllers:(NSArray*)viewControllers{

    if(self= [superinitWithFrame:frame]) {

        self.vcArray= viewControllers;

    }

    return self;

}

- (void)setVcArray:(NSArray*)vcArray{

    _vcArray= vcArray;

    _btnArray = [NSMutableArray array];

    [self confingTopView];

    [self configBottomView];

}

- (void)confingTopView{

    // 按钮宽度

    CGFloatbuttonWight =WindowWidth/ (_vcArray.count+1);

    // 按钮高度

    CGFloatbuttonhight =TopViewHight-4;

    CGRecttopViewFrame =CGRectMake(0,0,WindowWidth,TopViewHight);

    self.topView= [[UIViewalloc]initWithFrame:topViewFrame];

    [selfaddSubview:self.topView];

    // 提示器

    self.slideView = [[UIView alloc] initWithFrame:CGRectMake(IndicatorCenter, TopViewHight - 13, IndicatorWidth, IndicatorHeight)];

    [_slideView setBackgroundColor:[UIColor orangeColor]];

    [_topView  addSubview:self.slideView];

    // 添加按钮

    for(inti =0; i

        UIView*view = [[UIViewalloc]initWithFrame:CGRectMake(i * buttonWight,0, buttonWight, buttonhight)];

        UIButton*button = [[UIButtonalloc]initWithFrame:CGRectMake(0,0, buttonWight, buttonhight)];

        button.tag= i;

        NSString* buttonTitle =  [self.vcArray[i]allKeys][0];


        [buttonsetTitle:buttonTitle forState:UIControlStateNormal];

        button.titleLabel.font = [UIFont systemFontOfSize:15.0];

        [buttonsetTitleColor:HexUIColorFromRGB(0x333333, 0.6) forState:UIControlStateNormal];


        if(i ==0) {

            [buttonsetTitleColor:HexUIColorFromRGB(0x333333, 1.0) forState:UIControlStateNormal];

            button.titleLabel.font = [UIFont systemFontOfSize:17.0];

        }


        [buttonaddTarget:self action:@selector(tabButton:) forControlEvents:UIControlEventTouchUpInside];

        [viewaddSubview:button];


        [_btnArrayaddObject:button];

        [_topViewaddSubview:view];

    }

}

- (void)configBottomView{

    CGRect  bottomScrollViewFrame =CGRectMake(0, TopViewHight, WindowWidth, WindowHeight - TopViewHight );

    self.bottomScrollView= [[UIScrollViewalloc]initWithFrame:bottomScrollViewFrame];


    [self addSubview:_bottomScrollView];


    for(inti =0; i

        CGRect  VCFrame =CGRectMake(i *WindowWidth,0,WindowWidth, bottomScrollViewFrame.size.height);

        NSString* key = [self.vcArray[i]allKeys][0];

        UIViewController* vc =_vcArray[i][key] ;

        vc.view.frame= VCFrame;

        [self.bottomScrollViewaddSubview:vc.view];

    }

    self.bottomScrollView.contentSize = CGSizeMake(self.vcArray.count * WindowWidth, 0);

    self.bottomScrollView.pagingEnabled = YES;

    self.bottomScrollView.showsHorizontalScrollIndicator = NO;

    self.bottomScrollView.showsVerticalScrollIndicator = NO;

    self.bottomScrollView.directionalLockEnabled = YES;

    self.bottomScrollView.bounces = NO;

    self.bottomScrollView.delegate =self;

}

-(void)scrollViewDidScroll:(UIScrollView*)scrollView{

    CGRectframe =_slideView.frame;

    frame.origin.x= scrollView.contentOffset.x/(_vcArray.count+1) +IndicatorCenter;

    _slideView.frame= frame;

    intpageNum = scrollView.contentOffset.x/WindowWidth;


    for(UIButton* btnin_btnArray) {

        if(btn.tag== pageNum ) {

            [btnsetTitleColor:HexUIColorFromRGB(0x333333, 1.0) forState:UIControlStateNormal];

            btn.titleLabel.font = [UIFont systemFontOfSize:17.0];

        }else{

            [btnsetTitleColor:HexUIColorFromRGB(0x333333, 0.6) forState:UIControlStateNormal];

            btn.titleLabel.font = [UIFont systemFontOfSize:15.0];

        }

    }

}

-(void)tabButton: (id) sender{

    UIButton*button = sender;

    [buttonsetTitleColor:HexUIColorFromRGB(0x333333, 1.0) forState:UIControlStateNormal];

    button.titleLabel.font = [UIFont systemFontOfSize:17.0];


    for(UIButton* btnin_btnArray) {

        if(button != btn ) {

            [btnsetTitleColor:HexUIColorFromRGB(0x333333, 0.6) forState:UIControlStateNormal];

            btn.titleLabel.font = [UIFont systemFontOfSize:15.0];

        }

    }

    [_bottomScrollView setContentOffset:CGPointMake(button.tag * WindowWidth, 0) animated:YES];

}

@end

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容