iOS开发 横向分页样式 可左右滑动或点击头部栏按钮进行页面切换
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
iOS开发 横向分页样式 可左右滑动或点击头部栏按钮进行页面切换的更多相关文章
- iOS开发之 -- 判断tableview/scrollview的滑动方法,及导航栏渐变的实现代码
开发的过程中,肯定会用到在视图想上滑动的时候,在导航处做一些操作,比如向上滑动的时候,做个动画,出现一个搜索框,或者其他的操作,那么我们怎么来判断它的滑动方向呢? 首先我们应该知道tableview继 ...
- iOS开发 设置状态栏样式
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
- iOS开发小技巧 - label中的文字添加点击事件
Label中的文字添加点击事件 GitHub地址:https://github.com/lyb5834/YBAttributeTextTapAction 以前老师讲过类似的功能,自己懒得回头看了,找了 ...
- iOS 开发中中 textView 作为子控件点击输入文本,然后退出文本的方式
方式1. 使用当双击输入的时候弹出键盘同时,使用手势和通知监听键盘的方法实现 代码如下: 1. 监听键盘通知 [[NSNotificationCenter defaultCenter] addObse ...
- IOS开发---菜鸟学习之路--(十八)-利用代理实现向上一级页面传递数据
其实我一开始是想实现微信的修改个人信息那样的效果 就是点击昵称,然后跳转到另外一个页面输入信息 但是细想发现微信的话应该是修改完一个信息后就保存了 而我做的项目可能需要输入多个数据之后再点击提交的. ...
- iOS开发UINavigation——导航控制器UINavigationController
iOS开发UINavigation系列一——导航栏UINavigtionBar摘要iOS中的导航条可以附着于导航控制器之中使用,也可以在controller中单独使用,这篇博客,主要讨论有关导航栏的使 ...
- ios开发——实用技术OC-Swift篇&触摸与手势识别
iOS开发学习之触摸事件和手势识别 iOS的输入事件 触摸事件 手势识别 手机摇晃 一.iOS的输入事件 触摸事件(滑动.点击) 运动事件(摇一摇.手机倾斜.行走),不需要人为参与的 远程控制 ...
- 提高iOS开发效率的第三方框架等--不断更新中。。。
1. Mantle Mantle 让我们能简化 Cocoa 和 Cocoa Touch 应用的 model 层.简单点说,程序中经常要进行网络请求,请求到得一般是 json 字符串,我们一般会建一个 ...
- iOS开发常用的第三方类库
在iOS开发中不可避免的会用到一些第三方类库,它们提供了很多实用的功能,使我们的开发变得更有效率:同时,也可以从它们的源代码中学习到很多有用的东西. Reachability 检测网络连接 用来检查网 ...
随机推荐
- hive笔记:转义字符的使用
hive中的转义符 Hadoop和Hive都是用UTF-8编码的,所以, 所有中文必须是UTF-8编码, 才能正常使用 备注:中文数据load到表里面, 如果字符集不同,很有可能全是乱码需要做转码的, ...
- c/c++ 模板与STL小例子系列<一 >自建Array数组
c/c++ 模板与STL小例子系列 自建Array数组 自建的Array数组,提供如下对外接口 方法 功能描述 Array() 无参数构造方法,构造元素个数为模板参数个的数组 Array(int le ...
- [Hive_11] Hive 的高级聚合函数
0. 说明 Hive 的高级聚合函数 union all | grouping sets | cube | rollup pv //page view 页面访问量 uv //user view 访问人 ...
- LeetCode算法题-Majority Element(Java实现)
这是悦乐书的第181次更新,第183篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第40题(顺位题号是169).给定大小为n的数组,找到数组中出现次数超过n/2的元素.假 ...
- 《Java大学教程》—第9章 软件质量
软件质量:可维护性.可靠性.健壮性.可用性. 9.3 可维护性系统维护(maintaining)是指根据需求的变化更新现有系统的过程 9.3.1 封装的重要性连锁反应:对系统某一部分的改变可能会 ...
- 关于陌生的依赖模块,如withStyles、react-apollo等
有自己不认识的依赖,可参考的学习方式: 1.各大技术分享网站的文章(最快) 2.npm官网下的文档(最全)
- [BZOJ 2759] 一个动态树好题
[BZOJ 2759] 一个动态树好题 题目描述 首先这是个基环树. 然后根节点一定会连出去一条非树边.通过一个环就可以解除根的答案,然后其他节点的答案就可以由根解出来. 因为要修改\(p_i\),所 ...
- Scrapy 框架 总结
总结: 1.中间件:下载中间件(拦截请求和响应) - process_request: - prceess_response: - process_exception: - 请求: - UA伪装: - ...
- Hive数据仓库工具安装
一.Hive介绍 Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单SQL查询功能,SQL语句转换为MapReduce任务进行运行. 优点是可以通过类S ...
- 笔记-Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)
打开后缀.apk的文件.即启动安装程序. //apkFilePath 文件路径 public void installAPK(String apkFilePath) { // 创建URI Uri ur ...