需求如下:
分页展示内容,可以通过手势滑动来切换点击,也可以使用点击title来切换页面,上下滑动的时候,我的订单会显示到导航栏中。
思路:
1、可以自己实现,写一个类集成UIScrollView,可以通过暴露以下方法来实现:
@interface TopSelectBar : UIScrollView
@property (nonatomic, copy) NSArray *titleArr;
@property (nonatomic, copy) void (^didSelectedAt)(NSInteger index);
@property (nonatomic, assign) NSInteger curIndex;
@property (nonatomic, assign) CGFloat itemWidth;
@property (nonatomic, strong) UIImageView *thumbView;
@property (nonatomic, assign) CGFloat thumbPos; //0~maxIndex
@property (nonatomic, strong) UIColor *thumbColor;
@property (nonatomic, assign) CGSize thumbSize;
@property (nonatomic, strong) UIColor *titleColor;
@property (nonatomic, strong) UIColor *titleSelColor;
@property (nonatomic, strong) UIView *bottomLine;
- (void)setTitle:(NSString*)title AtIndex:(NSInteger)index;
.m具体实现:
#define BaseTag 1024
@implementation TopSelectBar
{
NSInteger _tolCount;
NSArray *_items;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
self.backgroundColor = [UIColor whiteColor];
_thumbColor = [UIColor SCTextBlueColor];
_titleColor = [UIColor whiteColor];
_titleSelColor = [UIColor greenColor];
_curIndex = 0;
_thumbSize = CGSizeMake(60, 3);
_itemWidth = 70;
}
return self;
}
- (void)setItemWidth:(CGFloat)itemWidth {
_itemWidth = itemWidth;
[self initSubViews];
}
- (void)setTitleArr:(NSArray *)titleArr {
_titleArr = [titleArr copy];
[self initSubViews];
}
- (void)setThumbColor:(UIColor *)thumbColor {
_thumbColor = thumbColor;
if (_thumbView)
_thumbView.backgroundColor = thumbColor;
}
- (void)setThumbSize:(CGSize)thumbSize {
_thumbSize = thumbSize;
if (_thumbView) {
_thumbView.size = thumbSize;
_thumbView.top = self.height - _thumbSize.height;
}
[self updateThumbWithAnimate:NO];
}
- (void)setTitleColor:(UIColor *)titleColor {
_titleColor = titleColor;
for (NSInteger i = 0; i < _items.count; i++) {
UIButton *button = _items[i];
[button setTitleColor:titleColor forState:UIControlStateNormal];
}
}
- (void)setTitleSelColor:(UIColor *)titleSelColor {
_titleSelColor = titleSelColor;
for (NSInteger i = 0; i < _items.count; i++) {
UIButton *button = _items[i];
[button setTitleColor:titleSelColor forState:UIControlStateSelected];
}
}
- (void)initSubViews
{
[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
_thumbView = nil;
if (_titleArr.count <= 0)
return;
if (_items.count > 0) {
[_items makeObjectsPerformSelector:@selector(removeFromSuperview)];
_items = nil;
}
// self.contentMode = UIViewContentModeCenter;
self.contentSize = CGSizeMake(_titleArr.count * _itemWidth, CGRectGetHeight(self.frame));
NSMutableArray *temp = [[NSMutableArray alloc] init];
for (int i = 0; i < _titleArr.count; i++)
{
NSString *title = [_titleArr objectAtIndex:i];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = BaseTag + i;
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button setTitleColor:_titleColor forState:UIControlStateNormal];
[button setTitleColor:_titleSelColor forState:UIControlStateSelected];
button.frame = CGRectMake(_itemWidth * i, 0, _itemWidth, self.bounds.size.height);
button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[button addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
[temp addObject:button];
}
_items = temp;
_bottomLine = [[UIView alloc] initWithFrame:CGRectMake(-150, self.height - .5, self.contentSize.width + 300, .5)];
_bottomLine.backgroundColor = [UIColor whiteColor];
[self addSubview:_bottomLine];
if (!_thumbView) {
_thumbView = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.height - _thumbSize.height, _thumbSize.width, _thumbSize.height)];
_thumbView.backgroundColor = _thumbColor;
[self addSubview:_thumbView];
}
[self updateThumbWithAnimate:NO];
}
- (void)updateThumbWithAnimate:(BOOL)animate
{
CGFloat centerX = _curIndex * _itemWidth + _itemWidth / 2;
if (animate)
{
[UIView animateWithDuration:0.2 animations:^{
_thumbView.centerX = centerX;
}];
}
else
_thumbView.centerX = centerX;
}
- (void)setTitle:(NSString*)title AtIndex:(NSInteger)index
{
if (index < 0 || index > _items.count - 1)
return;
UIButton *button = [_items objectAtIndex:index];
[button setTitle:title forState:UIControlStateNormal];
}
- (void)setThumbPos:(CGFloat)thumbPos {
_thumbView.centerX = _itemWidth * thumbPos + _itemWidth / 2;
if (_thumbPos<=thumbPos) {
if (self.contentOffset.x>_itemWidth*_thumbPos) {
CGPoint po = CGPointMake(_itemWidth*_thumbPos, 0);
self.contentOffset = po;
}else {
if (self.contentOffset.x+kSCREEN_WIDTH<_itemWidth*_thumbPos+_itemWidth) {
CGPoint po = CGPointMake(_itemWidth*(_thumbPos+1)-kSCREEN_WIDTH, 0);
self.contentOffset = po;
}
}
}else {
if (kSCREEN_WIDTH<_itemWidth*_thumbPos-self.contentOffset.x) {
CGPoint po = CGPointMake(_itemWidth*(_thumbPos+1)-kSCREEN_WIDTH, 0);
self.contentOffset = po;
}else {
if (self.contentOffset.x>_itemWidth*_thumbPos) {
CGFloat hh;
if (self.contentOffset.x-_itemWidth<0) {
hh = 0;
}else {
hh = _itemWidth*(_thumbPos);
}
CGPoint po = CGPointMake(hh, 0);
self.contentOffset = po;
}
}
}
_thumbPos = thumbPos;
NSInteger index = roundf(thumbPos);
if (index != _curIndex) {
UIButton *oldSelBtn = _items[_curIndex];
oldSelBtn.selected = NO;
UIButton *selBtn = _items[index];
selBtn.selected = YES;
_curIndex = index;
}
}
- (void)updateScrollRect:(UIView*)view {
CGFloat offset = 0;
if ((view.left - self.contentOffset.x) < 0)
offset = view.left - self.contentOffset.x;
else if ((view.right - self.width) > 0)
offset = view.right - self.width;
if (offset != 0)
[self scrollRectToVisible:CGRectMake(self.contentOffset.x + offset, 0, self.width, self.height) animated:YES];
}
- (void)setCurIndex:(NSInteger)curIndex {
UIButton *oldSelBtn = _items[_curIndex];
oldSelBtn.selected = NO;
UIButton *selBtn = _items[curIndex];
selBtn.selected = YES;
_curIndex = curIndex;
[self updateThumbWithAnimate:YES];
[self updateScrollRect:selBtn];
if (_didSelectedAt)
_didSelectedAt(_curIndex);
}
- (void)tapAction:(id)sender
{
UIButton *button = (UIButton*)sender;
self.curIndex = button.tag - BaseTag;
}
具体使用如下:
- (TopSelectBar*)topBar {
if (!_topBar) {
_topBar = [[TopSelectBar alloc] initWithFrame:CGRectMake(0, 1, kSCREEN_WIDTH, 46)];
_topBar.backgroundColor = [UIColor whiteColor];
_topBar.itemWidth = kSCREEN_WIDTH/2;
_topBar.titleArr = @[@"基本信息",@"更多信息"];
_topBar.titleColor = [UIColor SCTextLightGrayColor];
_topBar.titleSelColor = [UIColor SCBlackColor];
_topBar.thumbSize = CGSizeMake(42, 3);
_topBar.thumbColor =[UIColor SCLightBlueColor];
@Weakify(self);
_topBar.didSelectedAt = ^(NSInteger index){
@Strongify(self);
if (index == 0) {
self.headView.hidden = NO;
self.mTableView.hidden = YES;
}else{
self.headView.hidden = YES;
self.mTableView.hidden = NO;
}
};
}
return _topBar;
}
2、可以直接使用第三方分页菜单控制器WMPageController.
作用
1)这是一个类似于UINavigationController 和 UITabBarController 的一个UIViewController 的一个管理类。
2)用来分页展示内容的,可以通过手势滑动来切换页面,也可以使用点击title来切换页面 是一个用来管理ViewController的一个类,将 它的subViewController设置为每一个ViewController ,然后将这些subViewController 放在ScrollView 上面,故称呼为一个ViewController 的一个管理类。
如何使用
1、由于该第三方库支持CocoaPods。在项目中直接pod即可。
github地址:WMPageController
platform :ios,'9.0'
target 'DemoTest1' do
pod 'WMPageController', '~> 1.6.4'
end
2、自定义一个ArtScrollView继承自UIScrollView,重写代理方法。
//返回YES,则可以多个手势一起触发方法,返回NO则为互斥(比如外层UIScrollView名为mainScroll内嵌的UIScrollView名为subScroll,当我们拖动subScroll时,mainScroll是不会响应手势的(多个手势默认是互斥的),当下面这个代理返回YES时,subScroll和mainScroll就能同时响应手势,同时滚动,这符合我们这里的需求)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
该ArtScrollView用在MainViewController中,解决UIScrollView嵌套的手势冲突问题。
3、创建一个控制器作为容器
@interface SQHoverPageViewController ()<UIScrollViewDelegate,WMPageControllerDelegate>
@property (nonatomic, strong) WMPageController *pageController;
@property (nonatomic, strong) ArtScrollView *containerScrollView;
@property (nonatomic, strong) UIView *bannerView;
@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) UILabel *navTitleLb;
@property (nonatomic, strong) UIView *contentView;
/*view*/
/*model*/
@property (nonatomic, assign) BOOL isTopIsCanNotMoveTabView;
@property (nonatomic, assign) BOOL isTopIsCanNotMoveTabViewPre;
@property (nonatomic, assign) BOOL scrollViewCanScroll; // 最底部的scrollView是否能滚动的标志
@property (nonatomic,copy) NSArray *childViewControllerClasses;
@property (nonatomic,copy) NSArray *childViewControllertitles;
@property (nonatomic,copy) NSArray *sids;
@property (nonatomic,copy) NSString *naviTitle;
@property (nonatomic,strong) NSMutableArray *last2OffsetY;
@property (nonatomic,assign) NSInteger index;
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"标题";
_canScroll = YES;
self.automaticallyAdjustsScrollViewInsets = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:kHomeLeaveTopNotification object:nil];
[self setupView];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.navigationController.navigationBar.alpha = 0;
}
- (void)setupView {
[self.view addSubview:self.containerScrollView];
[self.containerScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.leading.bottom.trailing.equalTo(self.view).offset(0);
}];
[self.containerScrollView addSubview:self.bannerView];
[self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.leading.trailing.equalTo(self.containerScrollView);
make.width.equalTo(self.containerScrollView);
make.height.mas_equalTo(200);
}];
[self.containerScrollView addSubview:self.contentView];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.bannerView.mas_bottom);
make.leading.trailing.bottom.equalTo(self.containerScrollView);
make.width.equalTo(self.containerScrollView);
make.height.mas_equalTo(kScreenHeight-64);
}];
[self.contentView addSubview:self.pageController.view];
self.pageController.viewFrame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-64);
}
#pragma mark - getter
- (ArtScrollView *)containerScrollView {
if (!_containerScrollView) {
_containerScrollView = [[ArtScrollView alloc] init];
_containerScrollView.delegate = self;
_containerScrollView.showsVerticalScrollIndicator = NO;
}
return _containerScrollView;
}
- (UIView *)bannerView {
if (!_bannerView) {
_bannerView = [[UIView alloc] init];
_bannerView.backgroundColor = [UIColor blueColor];
}
return _bannerView;
}
- (UIView *)contentView {
if (!_contentView) {
_contentView = [[UIView alloc] init];
_contentView.backgroundColor = [UIColor yellowColor];
}
return _contentView;
}
- (WMPageController *)pageController {
if (!_pageController) {
_pageController = [[WMPageController alloc] initWithViewControllerClasses:@[[ChildTableViewController class],[ChildTableViewController class],[ChildTableViewController class]] andTheirTitles:@[@"tab1",@"tab2",@"tab3"]];
_pageController.menuViewStyle = WMMenuViewStyleLine;
_pageController.menuHeight = 44;
_pageController.progressWidth = 20;
_pageController.titleSizeNormal = 15;
_pageController.titleSizeSelected = 15;
_pageController.titleColorNormal = [UIColor grayColor];
_pageController.titleColorSelected = [UIColor blueColor];
}
return _pageController;
}
#pragma mark - notification
-(void)acceptMsg:(NSNotification *)notification{
NSDictionary *userInfo = notification.userInfo;
NSString *canScroll = userInfo[@"canScroll"];
if ([canScroll isEqualToString:@"1"]) {
_canScroll = YES;
}
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat maxOffsetY = 136;
CGFloat offsetY = scrollView.contentOffset.y;
self.navigationController.navigationBar.alpha = offsetY/136;
if (offsetY>=maxOffsetY) {
scrollView.contentOffset = CGPointMake(0, maxOffsetY);
//NSLog(@"滑动到顶端");
[[NSNotificationCenter defaultCenter] postNotificationName:kHomeGoTopNotification object:nil userInfo:@{@"canScroll":@"1"}];
_canScroll = NO;
}else{
//NSLog(@"离开顶端");
if (!_canScroll) {
scrollView.contentOffset = CGPointMake(0, maxOffsetY);
}
}
}
// 移除通知
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
3、为WMPageController创建子控制器。
#import "ChildTableViewController.h"
@interface ChildTableViewController ()<UIScrollViewDelegate>
@property (nonatomic, assign) BOOL canScroll;
@end
@implementation ChildTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellid"];
// add notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:kHomeGoTopNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:kHomeLeaveTopNotification object:nil];//其中一个TAB离开顶部的时候,如果其他几个偏移量不为0的时候,要把他们都置为0
}
#pragma mark - notification
-(void)acceptMsg:(NSNotification *)notification{
NSString *notificationName = notification.name;
if ([notificationName isEqualToString:kHomeGoTopNotification]) {
NSDictionary *userInfo = notification.userInfo;
NSString *canScroll = userInfo[@"canScroll"];
if ([canScroll isEqualToString:@"1"]) {
self.canScroll = YES;
self.tableView.showsVerticalScrollIndicator = YES;
}
}else if([notificationName isEqualToString:kHomeLeaveTopNotification]){
self.tableView.contentOffset = CGPointZero;
self.canScroll = NO;
self.tableView.showsVerticalScrollIndicator = NO;
}
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (!self.canScroll) {
[scrollView setContentOffset:CGPointZero];
}
CGFloat offsetY = scrollView.contentOffset.y;
if (offsetY<0) {
[[NSNotificationCenter defaultCenter] postNotificationName:kHomeLeaveTopNotification object:nil userInfo:@{@"canScroll":@"1"}];
}
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellid" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
// 移除通知
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}