标签:
// 纯代码实现横向滚动的UIScrollView
// NeedListViewController.m
//
//
// Copyright © 2016年 All rights reserved.
//
#import "CFNeedListViewController.h"
#import "CFWaitingViewController.h"
#import "CFRespondedViewController.h"
#import "CFOrderdeViewController.h"
#import "CFCanceledViewController.h"
#import "UIBarButtonItem+Item.h"
@interface CFNeedListViewController ()<UIScrollViewDelegate>
/** 标签栏底部的红色指示器 */
@property (nonatomic, weak) UIView *indicatorView;
/** 当前选中的按钮 */
@property (nonatomic, weak) UIButton *selectedButton;
/** 顶部的所有标签 */
@property (nonatomic, weak) UIView *titlesView;
/** 底部的所有内容 */
@property (nonatomic, weak) UIScrollView *contentView;
@end
@implementation CFNeedListViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 设置导航栏
[self setupNav];
// 初始化子控制器
[self setupChildVces];
// 设置顶部的标签栏
[self setupTitlesView];
// 底部的scrollView
[self setupContentView];
}
/**
* 初始化子控制器
*/
- (void)setupChildVces
{
CFWaitingViewController * waiting = [[CFWaitingViewController alloc] init];
waiting.title = @"待响应";
[self addChildViewController:waiting];
CFRespondedViewController *responded = [[CFRespondedViewController alloc] init];
responded.title = @"已响应";
[self addChildViewController:responded];
CFOrderdeViewController *ordered = [[CFOrderdeViewController alloc] init];
ordered.title = @"已达成订单";
[self addChildViewController:ordered];
CFCanceledViewController *canceled = [[CFCanceledViewController alloc] init];
canceled.title = @"已取消";
[self addChildViewController:canceled];
}
/**
* 设置顶部的标签栏
*/
- (void)setupTitlesView
{
// 标签栏整体
UIView *titlesView = [[UIView alloc] init];
titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
titlesView.width = self.view.width;
titlesView.height = CFTitlesViewH;
titlesView.y = 0;
[self.view addSubview:titlesView];
self.titlesView = titlesView;
// 底部的橙色指示器
UIView *indicatorView = [[UIView alloc] init];
indicatorView.backgroundColor = [UIColor orangeColor];
indicatorView.height = 2;
indicatorView.tag = -1;
indicatorView.y = titlesView.height - indicatorView.height;
self.indicatorView = indicatorView;
// 内部的子标签
CGFloat width = titlesView.width / self.childViewControllers.count;
CGFloat height = titlesView.height;
for (NSInteger i = 0; i<self.childViewControllers.count; i++) {
UIButton *button = [[UIButton alloc] init];
button.tag = i;
button.height = height;
button.width = width;
button.x = i * width;
UIViewController * vc = self.childViewControllers[i];
[button setTitle:vc.title forState:UIControlStateNormal];
// [button layoutIfNeeded]; // 强制布局(强制更新子控件的frame)
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor orangeColor] forState:UIControlStateDisabled];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
[titlesView addSubview:button];
// 默认点击了第一个按钮
if (i == 0) {
button.enabled = NO;
self.selectedButton = button;
// 让按钮内部的label根据文字内容来计算尺寸
[button.titleLabel sizeToFit];
self.indicatorView.width = button.width;
self.indicatorView.centerX = button.centerX;
}
}
[titlesView addSubview:indicatorView];
}
- (void)titleClick:(UIButton *)button
{
// 修改按钮状态
self.selectedButton.enabled = YES;
button.enabled = NO;
self.selectedButton = button;
// 动画
[UIView animateWithDuration:0.25 animations:^{
self.indicatorView.width = button.width;
self.indicatorView.centerX = button.centerX;
}];
// 滚动
CGPoint offset = self.contentView.contentOffset;
offset.x = button.tag * self.contentView.width;
[self.contentView setContentOffset:offset animated:YES];
}
/**
* 底部的scrollView
*/
- (void)setupContentView
{
// 不要自动调整inset
self.automaticallyAdjustsScrollViewInsets = NO;
UIScrollView *contentView = [[UIScrollView alloc] init];
contentView.frame = self.view.bounds;
contentView.delegate = self;
contentView.pagingEnabled = YES;
[self.view insertSubview:contentView atIndex:0];
contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
self.contentView = contentView;
// 添加第一个控制器的view
[self scrollViewDidEndScrollingAnimation:contentView];
}
/**
* 设置导航栏
*/
- (void)setupNav
{
// 设置导航栏标题
self.title = @"我的发布";
// 设置导航栏右边的按钮
UIBarButtonItem * tag1 = [UIBarButtonItem itemWithImage:@"wshoucang" highImage:@"wshoucang" target:self action:@selector(tag1)];
UIBarButtonItem * tag2 = [UIBarButtonItem itemWithImage:@"wshoucang" highImage:@"wshoucang" target:self action:@selector(tag2)];
self.navigationItem.rightBarButtonItems = @[tag1,tag2];
}
#pragma mark - 两个右边UIBarButtonItem的点击事件
#warning 待开发!!!!!!!!
- (void)tag1
{
CFCLog(@"%s",__func__);
}
- (void)tag2
{
CFCLog(@"%s",__func__);
}
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
// 当前的索引
NSInteger index = scrollView.contentOffset.x / scrollView.width;
// 取出子控制器
UIViewController *vc = self.childViewControllers[index];
vc.view.x = scrollView.contentOffset.x;
vc.view.y = 0; // 设置控制器view的y值为0(默认是20)
vc.view.height = scrollView.height; // 设置控制器view的height值为整个屏幕的高度(默认是比屏幕高度少个20)
[scrollView addSubview:vc.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self scrollViewDidEndScrollingAnimation:scrollView];
// 点击按钮
NSInteger index = scrollView.contentOffset.x / scrollView.width;
[self titleClick:self.titlesView.subviews[index]];
}
@end
标签:
原文地址:http://www.cnblogs.com/LiLihongqiang/p/5822658.html