分页控制器,这个还不够?

之前一直想要找的分页效果一直找不到,就自己写了一个

大神求点评,先上效果图和层次图

xxxx.gif
segment.png

其他不说了,直接上代码吧
新建一个控制器,继承UIViewController,下面是.h文件,定义一些属性只是为了封装而已

#import <UIKit/UIKit.h>

@interface SegmentVC : UIViewController

/**导航栏标题->不需要的在viewDidload里面修改一下就好*/
@property (nonatomic, strong) NSString *headTitle;

/**未选择按钮字体颜色 -> 默认[UIColor lightGrayColor]*/
@property (nonatomic, strong) UIColor *behindTitleColor;

/**当前选中字体颜色 默认蓝色*/
@property (nonatomic, strong) UIColor *selectTitleColor;

/**滑动线条的颜色 默认蓝色*/
@property (nonatomic, strong) UIColor *pageLineColor;

@end

新建两个空白控制器做测试用,下面是.m的头文件和宏定义以及私有属性

#import "SegmentVC.h"
#import "TestViewController1.h"
#import "TestViewController2.h"

#define PageCount 5
#define kButton_h 50
#define kTag 1000

#define KSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define KSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

@interface SegmentVC ()<UIScrollViewDelegate>

/**分页里面的滚动视图*/
@property (nonatomic, strong) UIScrollView *scrollView;
/**选中按钮*/
@property (nonatomic, strong) UIButton *selectBtn;
/**选中按钮显示的视图*/
@property (nonatomic, strong) UIView *selectView;
/**颜色字体添加的视图*/
@property (nonatomic, strong) UIView *mainView;
/**当前视图页数*/
@property (nonatomic, assign) NSInteger currentPages;

//存放控制器view
@property (nonatomic, strong) NSArray *viewAry;
//文字数组
@property (nonatomic, strong) NSArray *titleAry;

/**底部滚动视图条*/
@property (nonatomic, strong) UIView *pageLine;

@end

懒加载和viewDidLoad

- (UIView *)pageLine {
    if (_pageLine == nil) {
        self.pageLine = [[UIView alloc]init];
        _pageLine.backgroundColor = self.pageLineColor ? self.pageLineColor : [UIColor blueColor];
    }
    return _pageLine;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        /**如果需要修改滚动视图frame在这修改*/
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kButton_h+64, KSCREEN_WIDTH, KSCREEN_HEIGHT-64-kButton_h)];
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.bounces = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.contentSize = CGSizeMake(KSCREEN_WIDTH * PageCount, KSCREEN_HEIGHT-64-kButton_h);
        
        _scrollView.backgroundColor = [UIColor orangeColor];
    }
    return _scrollView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.headTitle;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.scrollView];
    
    //假设控制器以及分页文字
    self.viewAry = @[[TestViewController1 new],[TestViewController2 new],[TestViewController1 new],[TestViewController2 new],[TestViewController1 new]];
    self.titleAry = @[@"测试一",@"测试二",@"测试三",@"测试四",@"测试五"];
    
    //设置控制的每一个子控制器
    [self setupChildViewControll];
    //设置分页按钮
    [self setupPageButton];
}

设置控制的每一个子控制器

- (void)setupChildViewControll {
    for (int i = 0; i < self.viewAry.count; i ++) {
        UIViewController *viewC = self.viewAry[i];
        [self addChildViewController:viewC];
        [_scrollView addSubview:viewC.view];
        viewC.view.frame = CGRectMake(KSCREEN_WIDTH * i, 0, KSCREEN_WIDTH, KSCREEN_HEIGHT);
    }
}

设置分页按钮以及底部滚动条

- (void)setupPageButton {
    //最底层label
    for (int i = 0; i < self.titleAry.count; i ++) {
        CGFloat xMargin = KSCREEN_WIDTH / self.titleAry.count * i;
        [self.view addSubview:[self returnLabel:self.titleAry[i]
                                       andFrame:CGRectMake(xMargin, 64, KSCREEN_WIDTH/self.titleAry.count, kButton_h)
                                       andColor:self.behindTitleColor ? self.behindTitleColor : [UIColor lightGrayColor]]];
    }
    //设置上面覆盖视图,以及显示字体文字颜色视图
    _selectView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, KSCREEN_WIDTH/self.titleAry.count, kButton_h)];
    _selectView.userInteractionEnabled = YES;
    _selectView.clipsToBounds = YES;//不显示超出本身frame的视图
    [self.view addSubview:_selectView];
    //添加覆盖视图里的label
    self.mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREEN_WIDTH, kButton_h)];
    self.mainView.userInteractionEnabled = YES;
    for (int i = 0; i < self.titleAry.count; i ++) {
        CGFloat xMargin = KSCREEN_WIDTH / self.titleAry.count * i;
        [self.mainView addSubview:[self returnLabel:self.titleAry[i]
                                         andFrame:CGRectMake(xMargin, 0, KSCREEN_WIDTH/self.titleAry.count, kButton_h)
                                         andColor:self.selectTitleColor ? self.selectTitleColor:[UIColor blueColor]]];
    }
    [_selectView addSubview:self.mainView];
    
    //添加最上面点击按钮
    for (int i = 0; i < self.titleAry.count; i ++) {
        CGFloat xMargin = KSCREEN_WIDTH / self.titleAry.count * i;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(xMargin, 64, KSCREEN_WIDTH/self.titleAry.count, kButton_h);
        button.tag = i + kTag;
        [button addTarget:self action:@selector(pageClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        if (i == 0) {
            self.selectBtn = button;
        }
    }
    
    //添加下方跟着滚动的线
    CGFloat lineWidth = KSCREEN_WIDTH / self.titleAry.count / 2;
    self.pageLine.frame = CGRectMake(lineWidth / 2, 64 + kButton_h - 2, lineWidth, 2);
    [self.view addSubview:self.pageLine];
}

//准备label
- (UILabel *)returnLabel:(NSString *)title andFrame:(CGRect)rect andColor:(UIColor *)color
{
    UILabel * label = [[UILabel alloc]initWithFrame:rect];
    label.font = [UIFont boldSystemFontOfSize:15];
    label.textColor = color;
    label.text = title;
    label.userInteractionEnabled = YES;
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

按钮点击事件

- (void)pageClick:(UIButton *)btn {
    self.currentPages = btn.tag - kTag;
    [self gotoCurrentPage];
}
- (void)gotoCurrentPage {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * self.currentPages;
    frame.origin.y = 0;
    frame.size = _scrollView.frame.size;
    [_scrollView scrollRectToVisible:frame animated:YES];
    
}

代理

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    //修改两个视图的frame
    CGRect rect = self.selectView.frame;
    rect.origin.x = (scrollView.contentOffset.x / KSCREEN_WIDTH) * (KSCREEN_WIDTH / self.titleAry.count);
    self.selectView.frame = rect;
    
    CGRect mainRect = self.mainView.frame;
    mainRect.origin.x = - (scrollView.contentOffset.x / KSCREEN_WIDTH) * (KSCREEN_WIDTH / self.titleAry.count);
    self.mainView.frame = mainRect;

    //修改下面线条的frame
    CGRect lineRect = _pageLine.frame;
    lineRect.origin.x = (scrollView.contentOffset.x / KSCREEN_WIDTH) * (KSCREEN_WIDTH / self.titleAry.count) + (KSCREEN_WIDTH / self.titleAry.count / 4);;
    _pageLine.frame = lineRect;
    
    //修改当前按钮
    UIButton *button = [self.view viewWithTag:self.currentPages + kTag];
    if ([self.selectBtn isEqual:button]) {
        return;
    }
    self.selectBtn = button;
}

我简单说一下这个顶部视图的结构吧,说的不好可以随便批评,😂。 1.最底部是5个label,label的字体默认设为灰色 2.再添加一个view,view的高和宽跟label一样,设置view不显示超出本身frame多视图:clipsToBounds = YES 3.在view里面添加一个midView,midView的高和宽等于整个顶部视图frame 4.在midView里添加5个hightLable,frame和text分别跟前面5个label一样,默认颜色蓝色(当前所选分页的字体颜色) 5.在最上面再添加3个按钮

感觉说的一塌糊涂,小白见谅。如果需要代码的话可以加群:515385179,新建的群,平时聊聊天,有时间逛都会收集一些代码放群共享,欢迎大神指点~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,943评论 4 60
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,047评论 25 707
  • 2014.10.02 厦门大学的同学来找我跟小冉玩,我们出去吃饭看电影聊天,在泉州这座寂寞的城市走走停停,心情也起...
    枚橙roro阅读 252评论 0 1
  • 一直想记录一些东西,为了某些无处安放的情绪,希望简书可以帮我达成愿望
    叶弥儿阅读 142评论 0 0
  • 1、iconfont批量下载: var icons = document.querySelectorAll('.p...
    jeatime6阅读 537评论 0 0