一款iOS分页控制视图 LXBPageView

效果图

请添加图片描述

实现代码

//.h 文件

@class LXBPageView;

@protocol LXBPageViewDataSource <NSObject>

///页面数量
- (NSInteger)subViewsNumberOfPageView:(LXBPageView *)pageView;

/// 某个位置的字视图
/// @param pageView 分页视图
/// @param index 索引
- (UIView *)subViewForPageView:(LXBPageView *)pageView
                       atIndex:(NSInteger)index;


@end

@protocol LXBPageViewDelegate <NSObject>

/// 分页视图中的滚动视图滚动
/// @param pageView 分页视图
/// @param scrollView scrollView
- (void)scrollViewInPageView:(LXBPageView *)pageView
                   didScroll:(UIScrollView *)scrollView;

/// 分页视图滚动到某个index
/// @param pageView 分页视图
/// @param index 索引
- (void)scrollViewInPageView:(LXBPageView *)pageView
            didScrollToIndex:(NSInteger)index;

@end

@interface LXBPageView : UIView

///数据源
@property (nonatomic, weak) id <LXBPageViewDataSource> dataSource;

///代理
@property (nonatomic, weak) id <LXBPageViewDelegate> delegate;

@property (nonatomic, assign) NSInteger currentIndex;

- (void)reloadData;
///展示某个index
- (void)showSubViewWithIndex:(NSInteger)index;

@end

//.m 代码

@interface LXBPageView () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation LXBPageView

- (instancetype)initWithFrame:(CGRect)frame
{
    
    
    if (self = [super initWithFrame:frame]) {
    
    
        [self setUpUI];
        [self setUpContraints];
    }
    return self;
}

- (void)setUpUI
{
    
    
    [self addSubview:self.scrollView];
}

- (void)setUpContraints
{
    
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    
    
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}

#pragma mark - public
- (void)reloadData
{
    
    
    NSInteger numberOfSubViews = 0;
    if (self.dataSource &&
        [self.dataSource respondsToSelector:@selector(subViewsNumberOfPageView:)]) {
    
    
        numberOfSubViews = [self.dataSource subViewsNumberOfPageView:self];
    }
    CGFloat width = CGRectGetWidth(self.scrollView.frame);
    self.scrollView.contentSize = CGSizeMake(width * numberOfSubViews, 0);
    [self showSubViewWithIndex:self.currentIndex];
}

- (void)showSubViewWithIndex:(NSInteger)index
{
    
    
    CGFloat width = CGRectGetWidth(self.scrollView.frame);
    [self.scrollView setContentOffset:CGPointMake(width * index, 0) animated:NO];
    [self _showSubViewAtIndex:index];
    if (self.delegate &&
        [self.delegate respondsToSelector:@selector(scrollViewInPageView:didScrollToIndex:)]) {
    
    
        [self.delegate scrollViewInPageView:self didScrollToIndex:index];
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    
    if (self.delegate &&
        [self.delegate respondsToSelector:@selector(scrollViewInPageView:didScroll:)]) {
    
    
        [self.delegate scrollViewInPageView:self didScroll:scrollView];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    
    CGFloat width = CGRectGetWidth(self.scrollView.frame);
    NSInteger index = (scrollView.contentOffset.x + 0.5) / width;
    [self _showSubViewAtIndex:index];
    if (self.delegate &&
        [self.delegate respondsToSelector:@selector(scrollViewInPageView:didScrollToIndex:)]) {
    
    
        [self.delegate scrollViewInPageView:self didScrollToIndex:index];
    }
}

#pragma mark - private
- (void)_showSubViewAtIndex:(NSInteger)index
{
    
    
    UIView *view = [self.dataSource subViewForPageView:self atIndex:index];
    if (![self.scrollView.subviews containsObject:view]) {
    
    
        CGFloat width = CGRectGetWidth(self.scrollView.frame);
        CGFloat height = CGRectGetHeight(self.scrollView.bounds);
        view.frame = CGRectMake(index * width, 0, width, height);
        [self.scrollView addSubview:view];
    }
}

#pragma mark - lazy load
- (UIScrollView *)scrollView
{
    
    
    if (!_scrollView) {
    
    
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

@end

使用方法

pod 'LXBPageView'

- (LXBPageView *)pageView
{
    
    
    if (!_pageView) {
    
    
        _pageView = [[LXBPageView alloc] init];
        _pageView.dataSource = self;
        _pageView.delegate = self;
    }
    return _pageView;
}

// 实现协议方法
#pragma mark - LBPageViewDelegate, LBPageViewDataSource

- (NSInteger)subViewsNumberOfPageView:(LXBPageView *)pageView
{
    
    
    return self.contentArray.count;
}

- (UIView *)subViewForPageView:(LXBPageView *)pageView atIndex:(NSInteger)index
{
    
    
    NSString *key = [NSString stringWithFormat:@"%ld",index];
    LBContentView *view = [self.pageViewDic valueForKey:key];
    if (view) {
    
    
        return view;
    } else {
    
    
        view = [[LBContentView alloc] init];
        view.backgroundColor = self.colorArray[index];
        view.content = self.contentArray[index];
        self.pageViewDic[key] = view;
        return view;
    }
}

- (void)scrollViewInPageView:(LXBPageView *)pageView didScroll:(UIScrollView *)scrollView
{
    
    
    
}

- (void)scrollViewInPageView:(LXBPageView *)pageView didScrollToIndex:(NSInteger)index
{
    
    
}

demo地址

おすすめ

転載: blog.csdn.net/LIUXIAOXIAOBO/article/details/121439163