UIPageViewController功能实现,用UIScrollView替换

多页面混合开发我之前都是用UIPageViewController的,我不需要删除或新的添加就是固定几个VC,所以使用没问题,
最近一个项目开发的时候,我用UIScrollView做了一个
效果是这样的

点击标题切换界面,界面滑动标题跟着变
点击界面上的按钮能push到下一个界面

UIPageViewController功能实现,用UIScrollView替换_第1张图片
屏幕快照 2018-01-26 下午4.50.51.png

代码在这里


UIPageViewController功能实现,用UIScrollView替换_第2张图片
屏幕快照 2018-01-26 下午4.47.39.png
1、LCYHeadTitlesView是上面的标题栏
#import 

@interface LCYHeadTitlesView : UIView
//点击事件我用block传出来
@property (nonatomic, strong) void(^LCYCurrentHeadTitlesChangedCallBack)(NSInteger index);

//初始化的时候穿进去标题
- (instancetype)initWithFrame:(CGRect)frame andTitles:(NSArray *)titlesArray;
//底部跟着变的线可以自己设置是否隐藏
- (void)setLineViewHidden:(BOOL)hidden;
//选中哪个
- (void)setSelectedIndex:(NSInteger)index;

@end
#import "LCYHeadTitlesView.h"

#define headTitlesnNormalFont [UIFont systemFontOfSize:14]
#define headTitlesnNormalColor [UIColor qmui_colorWithHexString:@"#333333"]
#define headTitlesSelectedColor [UIColor redColor]
#define headTitlesLineViewColor [UIColor redColor]
static CGFloat HeadTitlesButtonLeftRightMargin = 70;//按钮距离self的左右边距

@interface LCYHeadTitlesView ()

@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, assign) CGFloat buttonWidth;
@property (nonatomic, strong) NSMutableArray *buttonsArray;

@end

@implementation LCYHeadTitlesView

- (instancetype)initWithFrame:(CGRect)frame andTitles:(NSArray *)titlesArray{
    self = [super initWithFrame:frame];
    if (self) {
        self.buttonWidth = (SCREEN_WIDTH - HeadTitlesButtonLeftRightMargin * 2) / titlesArray.count;
        self.buttonsArray = [NSMutableArray array];
        for (int i = 0; i < titlesArray.count; i ++) {
            QMUIButton *button = [[QMUIButton alloc] initWithFrame:CGRectMake(HeadTitlesButtonLeftRightMargin + _buttonWidth * i, 0, _buttonWidth, frame.size.height)];
            [button setTitle:titlesArray[i] forState:UIControlStateNormal];
            [button setTitleColor:headTitlesnNormalColor forState:UIControlStateNormal];
            [button setTitleColor:headTitlesSelectedColor forState:UIControlStateSelected];
            button.titleLabel.font = headTitlesnNormalFont;
            button.tag = 10 + i;
            button.selected = NO;
            if (i == 0) {
                button.selected = YES;
            }
            [button addTarget:self action:@selector(buttonClickedAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.buttonsArray addObject:button];
            [self addSubview:button];
        }
        self.lineView = [[UIView alloc] initWithFrame:CGRectMake(HeadTitlesButtonLeftRightMargin, frame.size.height - 2, _buttonWidth, 1)];
        [self addSubview:_lineView];
        self.lineView.backgroundColor = headTitlesLineViewColor;
    }
    return self;
}

- (void)buttonClickedAction:(QMUIButton *)button{
    NSInteger index = button.tag - 10;
    _LCYCurrentHeadTitlesChangedCallBack(index);
    [self setSelectedIndex:index];
}

- (void)setSelectedIndex:(NSInteger)index{
    QMUIButton *selectedButton = [self.buttonsArray objectAtIndex:index];
    for (QMUIButton *btn in self.buttonsArray) {
        btn.selected = NO;
    }
    selectedButton.selected = YES;
    [UIView animateWithDuration:0.2 animations:^{
        CGRect frame = CGRectMake(HeadTitlesButtonLeftRightMargin + _buttonWidth * index, self.frame.size.height - 2, _buttonWidth, 1);
        self.lineView.frame = frame;
    }];
}

#pragma mark - method -----------------------

- (void)setLineViewHidden:(BOOL)hidden{
    self.lineView.hidden = hidden;
}

@end
2、PageView
#import "LCYBaseView.h"

@interface LCYPageView : LCYBaseView

@property (nonatomic, strong) UIScrollView *contentScrollView;

@end

#import "LCYPageView.h"
#import "LCYHeadTitlesView.h"

@interface LCYPageView () 

@property (nonatomic, strong) LCYHeadTitlesView *headTitlesView;
@property (nonatomic, strong) NSArray *headTitlesArray;

@end

@implementation LCYPageView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.headTitlesArray = @[@"标题1",@"标题2",@"标题3"];
        //导航栏
        [self.topBarView setTitle:@"测试"];
        [self.topBarView showLeft:LCYLEFTTYPEIMAGETEXT];
        self.topBarView.leftLabel.text = @"左";
        [self.topBarView showRight:LCYLEFTTYPEIMAGETEXT];
        self.topBarView.rightLabel.text = @"右";

        [self headTitlesView];
        [self contentScrollView];

        @WeakObj(self);
        [self.headTitlesView setLCYCurrentHeadTitlesChangedCallBack:^(NSInteger index) {
            @StrongObj(self);
            self.contentScrollView.contentOffset = CGPointMake(index * SCREEN_WIDTH, 0);
        }];
    }
    return self;
}

- (LCYHeadTitlesView *)headTitlesView{
    if (!_headTitlesView) {
        _headTitlesView = [[LCYHeadTitlesView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREEN_WIDTH, 40) andTitles:self.headTitlesArray];
        [self addSubview:_headTitlesView];
        [_headTitlesView setLineViewHidden:YES];
    }
    return _headTitlesView;
}

- (UIScrollView *)contentScrollView{
    if (!_contentScrollView) {
        _contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight + 40, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaTopHeight - 40)];
        _contentScrollView.backgroundColor = [UIColor redColor];
        [self addSubview:_contentScrollView];
        _contentScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * self.headTitlesArray.count, SCREEN_HEIGHT - SafeAreaTopHeight - 40);
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        _contentScrollView.pagingEnabled = YES;
        _contentScrollView.delegate = self;
    }
    return _contentScrollView;
}

#pragma mark - delegate -----------------------

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x/SCREEN_WIDTH;
    [self.headTitlesView setSelectedIndex:index];
}

@end
3、PageViewController
#import "LCYPageViewController.h"
#import "LCYPageView.h"
#import "TestViewController.h"

@interface LCYPageViewController ()

@property (nonatomic, strong) LCYPageView *pageView;

@property (nonatomic, strong) TestViewController *vc0;
@property (nonatomic, strong) TestViewController *vc1;
@property (nonatomic, strong) TestViewController *vc2;

@end

@implementation LCYPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initInterface];
    [self addPageSubviews];
}

- (void)initInterface{
    self.pageView = [[LCYPageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaTopHeight)];
    [self.view addSubview:self.pageView];
}

- (void)addPageSubviews{
    self.vc0 = [[TestViewController alloc] init];
    self.vc0.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaTopHeight - 40);
    self.vc1 = [[TestViewController alloc] init];
    self.vc1.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaTopHeight - 40);
    self.vc2 = [[TestViewController alloc] init];
    self.vc2.view.frame = CGRectMake(SCREEN_WIDTH * 2, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaTopHeight - 40);
    
    _vc0.view.backgroundColor = [UIColor orangeColor];
    _vc1.view.backgroundColor = [UIColor yellowColor];
    _vc2.view.backgroundColor = [UIColor greenColor];
    
    [self.pageView.contentScrollView addSubview:self.vc0.view];
    [self.pageView.contentScrollView addSubview:self.vc1.view];
    [self.pageView.contentScrollView addSubview:self.vc2.view];
    
    //这三句必须加这样点击TestViewController上界面的时候才能用NavigationController
    [self addChildViewController:self.vc0];
    [self addChildViewController:self.vc1];
    [self addChildViewController:self.vc2];
}

@end

你可能感兴趣的:(UIPageViewController功能实现,用UIScrollView替换)