iOS开发小轮子-自定义UIPageControl

145 阅读1分钟

Simulator Screen Shot - iPhone 11 Pro Max - 2020-05-07 at 14.38.51.png /// 直接上代码

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

/// <#Description#>
@interface SDCustomPageControl : UIView

/** 页码总数 */
@property (nonatomic, assign) NSUInteger numerOfPages;

/** 当前选中的下标 */
@property (nonatomic, assign) NSUInteger currentPage;

/** 未选中点的颜色 */
@property (nonatomic,strong) UIColor *pageIndicatorTintColor;

/** 当前选择点的颜色 */
@property (nonatomic,strong) UIColor *currentPageIndicatorTintColor;

/** 未选中点的尺寸 */
@property (nonatomic, assign) CGSize pageSize;

/** 点与点之间的间距 */
@property (nonatomic, assign) float pointMargin;


@end

NS_ASSUME_NONNULL_END
#import "SDCustomPageControl.h"

@interface SDCustomPageControl()

/** 当前点的宽度为其他点的宽带的倍数 */
@property (nonatomic, assign) float currentWidthMultiple;


@end

@implementation SDCustomPageControl
{
    NSUInteger _oldCurrentPage;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        _numerOfPages = 0;
        _currentPage = 0;
        _oldCurrentPage = _currentPage;
        _pageSize = CGSizeMake(6, 6);
        _pointMargin = 8;
        _pageIndicatorTintColor = [UIColor lightGrayColor];
        _currentPageIndicatorTintColor = [UIColor whiteColor];
        _currentWidthMultiple = 2;
    }
    return self;
}

- (void)setNumerOfPages:(NSUInteger)numerOfPages{
    _numerOfPages = numerOfPages;
    if(_numerOfPages == 0) return;
    [self createPageControl];
    
}


- (void)setCurrentPage:(NSUInteger)currentPage{
    _oldCurrentPage = _currentPage;
    _currentPage = currentPage;
    [self exchangeCureentPageViewWithNewCurrentPage:_currentPage oldCurrentPage:_oldCurrentPage];
}

- (void)setPageSize:(CGSize)pageSize{
    _pageSize = pageSize;
    [self createPageControl];
}

- (void)setCurrentPageIndicatorTintColor:(UIColor *)currentPageIndicatorTintColor{
    _currentPageIndicatorTintColor = currentPageIndicatorTintColor;
    [self createPageControl];

}

- (void)setPageIndicatorTintColor:(UIColor *)pageIndicatorTintColor{
    _pageIndicatorTintColor = pageIndicatorTintColor;
    [self createPageControl];

}

- (void)setPointMargin:(float)pointMargin{
    _pointMargin = pointMargin;
    [self createPageControl];

}

- (void)createPageControl{
    for (UIView * view in self.subviews) {
        [view removeFromSuperview];
    }
    
    CGFloat pageContentWidth = _numerOfPages * (_pageSize.width + _pointMargin);
    CGFloat startPointX = 0;
    CGFloat startPointY = 0;
    CGFloat superViewWidth = self.frame.size.width;
    CGFloat superViewHeight = self.frame.size.height;
    CGFloat pointHeight = _pageSize.height;
    
    if(superViewWidth > pageContentWidth){
        startPointX = superViewWidth/2 - pageContentWidth/2;
    }
    
    if(superViewHeight > pointHeight){
        startPointY = superViewHeight/2 - pointHeight/2;
    }
    
    for (int i = 0 ; i < _numerOfPages; i ++) {
        if(i == _currentPage){
            UIView *currenPageView = [[UIView alloc]init];
            CGFloat currenPageViewWidth = _pageSize.width * _currentWidthMultiple;
            CGFloat currenPageViewHeight = _pageSize.height;
            currenPageView.frame = CGRectMake(startPointX, startPointY, currenPageViewWidth, currenPageViewHeight);
            currenPageView.tag = i + 1000;
            currenPageView.backgroundColor = _currentPageIndicatorTintColor;
            currenPageView.layer.cornerRadius = 2;
            [self addSubview:currenPageView];
            startPointX = CGRectGetMaxX(currenPageView.frame) + _pointMargin;
        }else{
            UIView *pageView = [[UIView alloc]init];
            CGFloat pageViewWidth = _pageSize.width ;
            CGFloat pageViewHeight = _pageSize.height;
            pageView.frame = CGRectMake(startPointX, startPointY, pageViewWidth, pageViewHeight);
            pageView.tag = i + 1000;
            pageView.backgroundColor = _pageIndicatorTintColor;
            pageView.layer.cornerRadius = 2;

            [self addSubview:pageView];
            startPointX = CGRectGetMaxX(pageView.frame) + _pointMargin;
            
        }
    }
}

- (void)exchangeCureentPageViewWithNewCurrentPage:(NSUInteger)newCurrentPage oldCurrentPage:(NSUInteger)oldCurrentPage{
    UIView * oldCurrentPointView = [self viewWithTag:oldCurrentPage + 1000];
    UIView * newCurrentPointView = [self viewWithTag:newCurrentPage + 1000];
    oldCurrentPointView.backgroundColor = _pageIndicatorTintColor;
    newCurrentPointView.backgroundColor = _currentPageIndicatorTintColor;
    if (_currentWidthMultiple != 1) {
        CGRect oldPointFrame = oldCurrentPointView.frame;
        if(newCurrentPage < oldCurrentPage){
            oldPointFrame.origin.x += _pageSize.width * (_currentWidthMultiple - 1);
           
        }
        oldPointFrame.size.width = _pageSize.width;
        oldCurrentPointView.frame = oldPointFrame;
        CGRect newPointFrame = newCurrentPointView.frame;
        if(newCurrentPage > oldCurrentPage){
            newPointFrame.origin.x -= _pageSize.width * (_currentWidthMultiple - 1);
           
        }
        newPointFrame.size.width = _pageSize.width * _currentWidthMultiple;
        newCurrentPointView.frame = newPointFrame;

    
    }
}