iOS开发 横向分页样式 可左右滑动或点击头部栏按钮进行页面切换

不多说直接上效果图和代码

1.设置RootViewController为一个导航试图控制器

//  Copyright © 2016年 Chason. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
    [self.window makeKeyAndVisible];

    return YES;
}

2.ViewController

//  Copyright © 2016年 Chason. All rights reserved.
//

#import "ViewController.h"
#import "XLScrollViewer.h"
#import "TotalTableView.h"
#import "UnSolveTableView.h"
#import "SolvedTableView.h"
@interface ViewController ()
@property (nonatomic, strong) TotalTableView *total;
@property (nonatomic, strong) UnSolveTableView *unSolve;
@property (nonatomic, strong) SolvedTableView *solved;;
@property (nonatomic, strong) XLScrollViewer *headerScrollView;
@end
//手机屏幕的宽和高
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"处理列表";
    //添加头导航条
    //视图初始化
    CGRect frame = CGRectMake(0, 64, kScreenWidth, KScreenHeight - 64);
    self.total = [[TotalTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, KScreenHeight - 64) style:UITableViewStyleGrouped];
    self.unSolve = [[UnSolveTableView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, KScreenHeight - 64) style:UITableViewStyleGrouped];
    self.solved = [[SolvedTableView alloc] initWithFrame:CGRectMake(2 * kScreenWidth, 0, kScreenWidth, KScreenHeight - 64) style:UITableViewStyleGrouped];
    NSArray *views = @[self.total, self.unSolve, self.solved];
    NSArray *titles = @[@"全部", @"未处理", @"已处理"];
    
    //初始化headerScrollView
    self.headerScrollView = [XLScrollViewer scrollWithFrame:frame withViews:views withButtonNames:titles withThreeAnimation:211];
    //定义滚动条属性
    
    self.headerScrollView.xl_topBackColor = [UIColor whiteColor];
    self.headerScrollView.xl_sliderColor = [UIColor colorWithRed:18 / 255.0 green:129 / 255.0 blue:201 / 255.0 alpha:1.0];
    self.headerScrollView.xl_buttonColorNormal = [UIColor grayColor];
    self.headerScrollView.xl_buttonColorSelected = [UIColor colorWithRed:18 / 255.0 green:129 / 255.0 blue:201 / 255.0 alpha:1.0];
    //    self.headerScrollView.xl_buttonFont = 15;
    //    self.headerScrollView.xl_topHeight = 40;
    [self.view addSubview:self.headerScrollView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

3.自定义多个页面, 我这里自定义TableView, 实际上可以是各种类型的UIView

//  Copyright © 2016年 Chason. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TotalTableView : UITableView<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

//  Copyright © 2016年 Chason. All rights reserved.
//

#import "TotalTableView.h"
#import "DIYTableViewCell.h"
@implementation TotalTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.dataSource = self;
        self.delegate = self;
        self.dataArray = [[NSMutableArray alloc] initWithCapacity:1];
        NSArray *titleArray = [NSArray arrayWithObjects:@"天花板坏了", @"天然气泄漏", @"下水道堵塞", @"地板损坏", @"防盗网破裂", @"门铃坏了", @"窗子坏了", @"厕所坏了", nil];
        NSArray *dateArray = [NSArray arrayWithObjects:@"2016-4-11", @"2016-4-11", @"2014-4-10", @"2014-4-5", @"2014-4-5", @"2014-4-1", @"2013-10-9", @"2013-9-5", nil];
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.dataArray addObject:titleArray];
        [self.dataArray addObject:dateArray];
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray[0] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DIYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"total"];
    if (cell == nil) {
        cell = [[DIYTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"total"];
    }
    cell.title.text = self.dataArray[0][indexPath.row];
    cell.date.text = self.dataArray[1][indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}

@end

//  Copyright © 2016年 Chason. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UnSolveTableView : UITableView<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

//  Copyright © 2016年 Chason. All rights reserved.
//

#import "UnSolveTableView.h"
#import "DIYTableViewCell.h"

@implementation UnSolveTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.dataSource = self;
        self.delegate = self;
        self.backgroundColor = [UIColor redColor];
        self.dataArray = [[NSMutableArray alloc] initWithCapacity:1];
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        NSArray *titleArray = [NSArray arrayWithObjects:@"天花板坏了", @"天然气泄漏", @"下水道堵塞", @"地板损坏", @"防盗网破裂", @"门铃坏了", @"窗子坏了", @"厕所坏了", nil];
        NSArray *dateArray = [NSArray arrayWithObjects:@"2016-4-11", @"2016-4-11", @"2014-4-10", @"2014-4-5", @"2014-4-5", @"2014-4-1", @"2013-10-9", @"2013-9-5", nil];
        
        [self.dataArray addObject:titleArray];
        [self.dataArray addObject:dateArray];
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray[0] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DIYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"total"];
    if (cell == nil) {
        cell = [[DIYTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"total"];
    }
    cell.title.text = self.dataArray[0][indexPath.row];
    cell.date.text = self.dataArray[1][indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}


@end

//  Copyright © 2016年 Chason. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SolvedTableView : UITableView<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

//  Copyright © 2016年 Chason. All rights reserved.
//

#import "SolvedTableView.h"
#import "DIYTableViewCell.h"

@implementation SolvedTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.dataSource = self;
        self.delegate = self;
        self.backgroundColor = [UIColor greenColor];
        self.dataArray = [[NSMutableArray alloc] initWithCapacity:1];
        NSArray *titleArray = [NSArray arrayWithObjects:@"天花板坏了", @"天然气泄漏", @"下水道堵塞", @"地板损坏", @"防盗网破裂", @"门铃坏了", @"窗子坏了", @"厕所坏了", nil];
        NSArray *dateArray = [NSArray arrayWithObjects:@"2016-4-11", @"2016-4-11", @"2014-4-10", @"2014-4-5", @"2014-4-5", @"2014-4-1", @"2013-10-9", @"2013-9-5", nil];
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.dataArray addObject:titleArray];
        [self.dataArray addObject:dateArray];
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray[0] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DIYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"total"];
    if (cell == nil) {
        cell = [[DIYTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"total"];
    }
    cell.title.text = self.dataArray[0][indexPath.row];
    cell.date.text = self.dataArray[1][indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}


@end

//自定义cell

//  Copyright © 2016年 Chason. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DIYTableViewCell : UITableViewCell
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UILabel *title;
@property (nonatomic, strong) UILabel *date;
@end

//  Copyright © 2016年 Chason. All rights reserved.
//

#import "DIYTableViewCell.h"
//手机屏幕的宽和高
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height
@implementation DIYTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backView = [[UIView alloc] initWithFrame:CGRectMake(15, 5, kScreenWidth - 30, 60)];
        self.backView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        self.backView.layer.cornerRadius = 10;
        [self addSubview:self.backView];
        self.title  = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, kScreenWidth - 40, 15)];
        self.title.font = [UIFont systemFontOfSize:17];
        [self.backView addSubview:self.title];
        self.date = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, kScreenWidth - 40, 15)];
        self.date.font = [UIFont systemFontOfSize:16];
        self.date.textAlignment = NSTextAlignmentRight;
        [self.backView addSubview:self.date];
    }
    return self;
}

@end

4.重头戏, 封装的滚动控制条, 只是我之前看一位大神写的,忘记出处了,特此申明,表示抱歉, 再次表示膜拜...

#import <UIKit/UIKit.h>

@interface XLScrollViewer : UIView

#pragma mark -使用XLScrollView
/**
 *     使用XLScrollView,数组如果填nil,则为默认,choose处填写三位数字,代表选择与否,1代表选择,2代表不选择。例如:你准备选择第1种和第3种动画效果,则填写121
 */
-(instancetype)initWithFrame:(CGRect)frame withViews:(NSArray *)views withButtonNames:(NSArray *)names withThreeAnimation:(int)choose;//实例方法
+(instancetype)scrollWithFrame:(CGRect)frame withViews:(NSArray *)views withButtonNames:(NSArray *)names withThreeAnimation:(int)choose;//类方法


#pragma mark -三种动画效果,按照顺序来
/**
 *     1、滑动视图时是否展示头部控制条 移动按钮 的动画效果,默认为 NO
 */
@property (nonatomic) BOOL xl_isMoveButton;
/**
 *     2、点击未被选中的按钮时时候展示 缩放按钮 的动画效果,默认为 NO
 */
@property (nonatomic) BOOL xl_isScaleButton;
/**
 *     3、滑动视图时是否展示头部控制条 滑动滑块 的动画效果,默认为 NO
 */
@property (nonatomic) BOOL xl_isMoveSlider;



/**
 *     要展示在scollView中的view,一页一视图,可在new出来后直接放入数组
 *     如果为空,默认为 红、橙、黄三个背景色的view
 */
@property (nonatomic ,strong) NSArray *xl_views;
/**
 *     头部控制条按钮的名称,存放在此数组中,NSString直接赋值,默认字号下不宜超过3个汉字
 *     如果为空,默认为  @[@"田馥甄",@"章晓亮",@"哈哈哈"]
 */
@property (nonatomic ,copy) NSArray *xl_buttonNames;


#pragma mark -各种可供自定义的属性
/**
 *     头部控制条的高度,默认为50
 */
@property (nonatomic ,assign) CGFloat xl_topHeight;
/**
 *     头部控制条按钮的字号,默认为18
 */
@property (nonatomic ,assign) CGFloat xl_buttonFont;
/**
 *     头部控制条按钮在UIControlStateNormal状态下的文字颜色,默认为黑色
 */
@property (nonatomic ,strong) UIColor *xl_buttonColorNormal;
/**
 *     头部控制条按钮在UIControlStateSelected状态下的文字颜色,默认为白色
 */
@property (nonatomic ,strong) UIColor *xl_buttonColorSelected;
/**
 *     头部控制条的背景颜色,默认为浅灰色lightGrayColor
 */
@property (nonatomic ,strong) UIColor *xl_topBackColor;
/**
 *     头部控制条的背景图片,默认为无
 */
@property (nonatomic ,strong) UIImage *xl_topBackImage;
/**
 *     头部控制条里的滑块颜色,默认为蓝色
 */
@property (nonatomic ,strong) UIColor *xl_sliderColor;
/**
 *     微调滑块相对于按钮的坐标x值,默认为10
 */
@property (nonatomic ,assign) CGFloat xl_buttonToSlider;
#pragma mark 由于滑块的宽度根据按钮文字内容自适应,若要调整滑块的宽度,只需在xl_buttonNames数组中的字符串左右两边加上空格即可
/**
 *     头部控制条里的滑块的高度,默认为2
 */
@property (nonatomic ,assign) CGFloat xl_sliderHeight;
/**
 *     头部控制条滑块是否设置圆角,默认为 NO
 */
@property (nonatomic) BOOL xl_isSliderCorner;
/**
 *     设置头部控制条圆角比例,默认为5
 */
@property (nonatomic ,assign) CGFloat xl_sliderCorner;

@end

#define screen_width [UIScreen mainScreen].bounds.size.width

#import "XLScrollViewer.h"

@interface XLScrollViewer ()<UIScrollViewDelegate>
{
    int _x;
    CGFloat _x0;
}
@property(nonatomic,strong)UIScrollView *scroll1;
@property(nonatomic,strong)UIScrollView *scroll2;
@property(nonatomic,strong)UIView *view2;

@property(nonatomic ,strong)NSMutableArray *buttons;

@end

@implementation XLScrollViewer

-(instancetype)initWithFrame:(CGRect)frame withViews:(NSArray *)views withButtonNames:(NSArray *)names withThreeAnimation:(int)choose{
    self =[super initWithFrame:frame];
    if (self) {
        self.xl_views =views;
        self.xl_buttonNames =names;
        NSString *temp =[NSString stringWithFormat:@"%d",choose];
        NSArray *arr =@[@"111",@"112",@"121",@"211",@"122",@"212",@"221",@"222"];
        for (NSString *str in arr) {
            if ([temp isEqualToString:str]) {
                if ([[temp substringWithRange:NSMakeRange(0, 1)] isEqual:@"1"]) {
                    self.xl_isMoveButton =YES;
                }
                if ([[temp substringWithRange:NSMakeRange(1, 1)] isEqual:@"1"]) {
                    self.xl_isScaleButton =YES;
                }
                if ([[temp substringWithRange:NSMakeRange(2, 1)] isEqual:@"1"]) {
                    self.xl_isMoveSlider =YES;
                }
            }
        }
    }
    return self;
}
+(instancetype)scrollWithFrame:(CGRect)frame withViews:(NSArray *)views withButtonNames:(NSArray *)names withThreeAnimation:(int)choose{
    return [[self alloc]initWithFrame:frame withViews:views withButtonNames:names withThreeAnimation:choose];
}

-(void)drawRect:(CGRect)rect {
    
    [self addAll];
}
-(void)addAll {
    if ((self.xl_buttonNames.count || self.xl_views.count) && self.xl_buttonNames.count !=self.xl_views.count) {
        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"XLScroll友情提醒!" message:@"您填写的按钮数与视图数不一致,请仔细检查代码" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alert show];
    }else {
        self.xl_buttonNames =self.xl_buttonNames?self.xl_buttonNames:@[@"田馥甄",@"章晓亮",@"哈哈哈"];
        
        [self addScroll2];
        [self addScroll1];
    }
}
-(void)addScroll1{
    
    
    
    self.scroll1 =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, screen_width, self.xl_topHeight?self.xl_topHeight:50)];
    
    if (self.xl_buttonNames.count <=5) {
        self.scroll1.contentSize =CGSizeMake(screen_width, 0);
    }else {
        self.scroll1.contentSize =CGSizeMake(screen_width/5*self.xl_buttonNames.count, 0);
    }
    if (self.xl_topBackImage) {
        self.scroll1.backgroundColor =[UIColor clearColor];
        UIImageView *temp =[[UIImageView alloc]initWithFrame:self.scroll1.frame];
        temp.image =self.xl_topBackImage;
        [self insertSubview:temp belowSubview:self.scroll1];
    }else {
        self.scroll1.backgroundColor =self.xl_topBackColor?self.xl_topBackColor:[UIColor lightGrayColor];
    }
    
    self.scroll1.showsHorizontalScrollIndicator =NO;
    self.scroll1.showsVerticalScrollIndicator =NO;
    self.scroll1.bounces =NO;
    self.scroll1.contentOffset=CGPointZero;
    self.scroll1.scrollsToTop =NO;
    
    self.buttons =[NSMutableArray array];
    for (int i =0; i<self.xl_buttonNames.count; i++) {
        UIButton *temp =[UIButton buttonWithType:UIButtonTypeCustom];
        if (self.xl_buttonNames.count <=5) {
            temp.frame =CGRectMake(screen_width/self.xl_buttonNames.count*i, 0, screen_width/self.xl_buttonNames.count, self.xl_topHeight?self.xl_topHeight:50);
        }else {
            temp.frame =CGRectMake(screen_width/5*i, 0, screen_width/5, self.xl_topHeight?self.xl_topHeight:50);
        }
        temp.titleLabel.font =[UIFont systemFontOfSize:self.xl_buttonFont?self.xl_buttonFont:18];
        [temp setTitle:self.xl_buttonNames[i] forState:UIControlStateNormal];
        [temp setTitleColor:self.xl_buttonColorNormal?self.xl_buttonColorNormal:[UIColor blackColor] forState:UIControlStateNormal];
        [temp setTitleColor:self.xl_buttonColorSelected?self.xl_buttonColorSelected:[UIColor whiteColor] forState:UIControlStateSelected];
        if (i == 0) {
            temp.selected =YES;
            [temp setTitleColor:self.xl_buttonColorSelected?self.xl_buttonColorSelected:[UIColor whiteColor] forState:UIControlStateNormal];
        }
        [temp addTarget:self action:@selector(changed:) forControlEvents:UIControlEventTouchUpInside];
        [self.scroll1 addSubview:temp];
        [self.buttons addObject:temp];
    }
    
    CGSize size0 =[self.xl_buttonNames[0] sizeWithAttributes:@{NSFontAttributeName :[UIFont systemFontOfSize:self.xl_buttonFont?self.xl_buttonFont:18]}];
    UIButton *button0 =self.buttons[0];
    _x0 =button0.center.x -size0.width/2;
    self.view2 =[[UIView alloc]initWithFrame:CGRectMake(_x0,CGRectGetMaxY(button0.frame)-(self.xl_buttonToSlider?self.xl_buttonToSlider:10), size0.width, self.xl_sliderHeight?self.xl_sliderHeight:2)];
    self.view2.backgroundColor =self.xl_sliderColor?self.xl_sliderColor:[UIColor colorWithRed:73 / 255.0 green:157 / 255.0 blue:242 / 255.0 alpha:1.0];
    if (self.xl_isSliderCorner) {
        [self.view2.layer setCornerRadius:self.xl_sliderCorner?self.xl_sliderCorner:5];
    }
    //UIImageView *lineView = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.view2.frame.origin.y + 1, screen_width, 1)];
    //lineView.image = [UIImage imageNamed:@"line.png"];
    //[self.scroll1 insertSubview:lineView atIndex:0];
    [self.scroll1 insertSubview:self.view2 atIndex:0];
    
    [self addSubview:self.scroll1];
}

-(void)addScroll2{
    self.scroll2 =[[UIScrollView alloc]initWithFrame:CGRectMake(0, self.xl_topHeight?self.xl_topHeight:50, screen_width, self.frame.size.height -(self.xl_topHeight?self.xl_topHeight:50))];
    self.scroll2.contentOffset=CGPointZero;
    self.scroll2.contentSize=CGSizeMake(screen_width*self.xl_buttonNames.count, 0);
    self.scroll2.showsHorizontalScrollIndicator =NO;
    self.scroll2.showsVerticalScrollIndicator =NO;
    self.scroll2.delegate =self;
    self.scroll2.pagingEnabled =YES;
    self.scroll2.bounces =NO;
    self.scroll2.scrollsToTop =NO;
    
    for (int i =0; i<self.xl_buttonNames.count; i++) {
        
        if (!self.xl_views) {
            UIView *temp =[[UIView alloc]initWithFrame:(CGRect){{screen_width*i, 0},self.scroll2.frame.size}];
            NSArray *cls =@[[UIColor redColor],[UIColor orangeColor],[UIColor yellowColor]];
            temp.backgroundColor =cls[i];
            [self.scroll2 addSubview:temp];
        }else {
            UIView *temp = self.xl_views[i];
            temp.frame =(CGRect){{screen_width*i, 0},self.scroll2.frame.size};
            [self.scroll2 addSubview:temp];
        }
    }
   
    [self addSubview:self.scroll2];
}

-(void)changed:(UIButton *)button{
    
    if (self.xl_isScaleButton) {
        if (!button.selected) {
            [UIView animateWithDuration:0.2 animations:^{
                button.transform =CGAffineTransformScale(button.transform, 0.7, 0.7);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.2 animations:^{
                    button.transform =CGAffineTransformScale(button.transform, 1/0.6, 1/0.6);
                } completion:^(BOOL finished) {
                    [UIView animateWithDuration:0.2 animations:^{
                        button.transform =CGAffineTransformScale(button.transform, 1/0.7*0.6, 1/0.7*0.6);
                    }];
                }];
            }];
        }
    }
    
    for (UIButton *temp in self.buttons) {
        
        if (temp.selected && temp !=button) {
            temp.selected =NO;
        }
        if (temp ==button) {
            [temp setTitleColor:self.xl_buttonColorSelected?self.xl_buttonColorSelected:[UIColor whiteColor] forState:UIControlStateNormal];
        }else {
            [temp setTitleColor:self.xl_buttonColorNormal?self.xl_buttonColorNormal:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    button.selected =YES;
    
    self.scroll2.delegate =nil;
    
    if (self.xl_buttonNames.count <=5) {
        self.scroll2.contentOffset =CGPointMake(button.center.x*self.xl_buttonNames.count -screen_width/2, 0);
    }else {
        self.scroll2.contentOffset =CGPointMake(button.center.x*5 -screen_width/2, 0);
    }
    CGSize size =[button.titleLabel.text sizeWithAttributes:@{NSFontAttributeName :[UIFont systemFontOfSize:self.xl_buttonFont?self.xl_buttonFont:18]}];
    if (self.xl_isMoveSlider) {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect rect =self.view2.frame;
            rect.size.width =size.width;
            self.view2.frame =rect;
            self.view2.transform =CGAffineTransformMakeTranslation(button.frame.origin.x +button.frame.size.width/2 -size.width/2 -_x0, 0);
        }];
    }else {
        CGRect rect =self.view2.frame;
        rect.size.width =size.width;
        self.view2.frame =rect;
        self.view2.transform =CGAffineTransformMakeTranslation(button.frame.origin.x +button.frame.size.width/2 -size.width/2 -_x0, 0);
    }
    
    
    self.scroll2.delegate =self;
    
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    _x =scrollView.contentOffset.x/screen_width;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    UIButton *button =self.buttons[_x];
    [button setTitleColor:self.xl_buttonColorNormal?self.xl_buttonColorNormal:[UIColor blackColor] forState:UIControlStateNormal];
    
    CGPoint point=self.scroll2.contentOffset;
    point.y =0;
    self.scroll2.contentOffset =point;
    
    if (self.xl_isMoveButton) {
        if (self.xl_buttonNames.count <=5) {
            button.transform =CGAffineTransformMakeTranslation((scrollView.contentOffset.x -button.frame.size.width*self.xl_buttonNames.count*_x)/self.xl_buttonNames.count/3, 0);
            self.view2.transform =CGAffineTransformMakeTranslation(scrollView.contentOffset.x/self.xl_buttonNames.count, 0);
        }else {
            button.transform =CGAffineTransformMakeTranslation((scrollView.contentOffset.x -button.frame.size.width*5*_x)/5/3, 0);
            self.view2.transform =CGAffineTransformMakeTranslation(scrollView.contentOffset.x/5, 0);
        }
    }else {
        if (self.xl_buttonNames.count <=5) {
            self.view2.transform =CGAffineTransformMakeTranslation(scrollView.contentOffset.x/self.xl_buttonNames.count, 0);
        }else {
            self.view2.transform =CGAffineTransformMakeTranslation(scrollView.contentOffset.x/5, 0);
        }
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    
    for (UIButton *temp in self.buttons) {
        
        [UIView animateWithDuration:0.2 animations:^{
            
            temp.transform =CGAffineTransformMakeTranslation(0, 0);
        }];
        
        if (temp.selected) {
            temp.selected =NO;
        }
        
        int x1 =temp.frame.origin.x;
        int x2 =0;
        if (self.xl_buttonNames.count <=5) {
            x2 =scrollView.contentOffset.x/self.xl_buttonNames.count;
        }else {
            x2 =scrollView.contentOffset.x/5;
        }
        
        
        if (x1 == x2) {
            
            temp.selected =YES;
            [temp setTitleColor:self.xl_buttonColorSelected?self.xl_buttonColorSelected:[UIColor whiteColor] forState:UIControlStateNormal];
            
            CGSize size =[temp.titleLabel.text sizeWithAttributes:@{NSFontAttributeName :[UIFont systemFontOfSize:self.xl_buttonFont?self.xl_buttonFont:18]}];
            [UIView animateWithDuration:0.2 animations:^{
                CGRect rect =self.view2.frame;
                rect.size.width =size.width;
                self.view2.frame =rect;
                self.view2.transform =CGAffineTransformMakeTranslation(temp.frame.origin.x +temp.frame.size.width/2 -size.width/2 -_x0, 0);
            }];
        }
    }
    
    if (self.buttons.count >5) {
        UIButton *button =self.buttons[_x];
        int xAfter =scrollView.contentOffset.x/screen_width;
        if (_x<xAfter) {
            
            if (_x>=2 && _x<=self.xl_buttonNames.count-4 ) {
                [UIView animateWithDuration:0.2 animations:^{
                    self.scroll1.contentOffset =CGPointMake(button.center.x -screen_width/5*1.5, 0);
                }];
            }
            if ((self.buttons.count ==6)|7 &&_x>3) {
                if ((_x ==4)|5) {
                    [UIView animateWithDuration:0.2 animations:^{
                        self.scroll1.contentOffset =CGPointMake(button.frame.size.width*(self.buttons.count==7?2:1), 0);
                    }];
                }
            }
        }else if (_x>xAfter) {
            
            if (_x>=3 && _x<=self.xl_buttonNames.count-2 ) {
                [UIView animateWithDuration:0.2 animations:^{
                    self.scroll1.contentOffset =CGPointMake(button.center.x -screen_width/5*3.5, 0);
                }];
            }
            if ((self.buttons.count ==6)|7 &&_x<3) {
                if ((_x ==1)|2) {
                    [UIView animateWithDuration:0.2 animations:^{
                        self.scroll1.contentOffset =CGPointMake(0, 0);
                    }];
                }
            }
        }
    }
    
}

@end

如果您觉得博文对您有帮助, 希望您能打发给博主一点茶水钱, 您的支持就是博主最大的动力!

版权归博主所有, 如有转载请注明出处!
原文地址:https://www.cnblogs.com/chasonCH/p/5390893.html