ios 带点和条状的PageControl

#import <UIKit/UIKit.h>

 NS_ASSUME_NONNULL_BEGIN

 @interface ZYSPageControl : UIControl

 @property (nonatomic, assign) NSInteger numberOfPages;
 @property (nonatomic, assign) NSInteger currentPage;
 @property (nonatomic, strong) UIColor *otherColor;
 @property (nonatomic, strong) UIColor *currentColor;
 @property (nonatomic, assign) CGSize controlSize;
 @property (nonatomic, assign) CGFloat controlSpacing;

@end






  #import "ZYSPageControl.h"

 @implementation ZYSPageControl

 - (instancetype)init {
   if (self = [super init]) {
     [self initSetUp];
   }
 return self;
}
- (void)layoutSubviews {
   [super layoutSubviews];
   [self createPointView];
}
- (void)initSetUp {
    self.backgroundColor = [UIColor clearColor];
    _numberOfPages = 2;
    _currentPage = 0;
    _controlSize = CGSizeMake(10, 10);
    _controlSpacing = 15;
    _otherColor = ZYSColor(0x414245);
    _currentColor = ZYSColor(0x4B6AFF);
}

-(void)setCurrentPage:(NSInteger)currentPage{
  if (_currentPage == currentPage) return;
  [self exchangeCurrentView:_currentPage newP:currentPage];
  _currentPage = currentPage;
}

- (void)createPointView {
     [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
     [self creatDotWithSize:CGSizeMake(35, 10) withOtherDotSize:CGSizeMake(10, 10)];
  }
- (void)creatDotWithSize:(CGSize)currentDotSize withOtherDotSize:(CGSize)otherDotSize {
     CGFloat startX = 0;
     CGFloat startY = 0;
     CGFloat currentDotSizeW = currentDotSize.width;
     CGFloat currentDotSizeH = currentDotSize.height;
     CGFloat otherDotSizeW = otherDotSize.width;
    CGFloat otherDotSizeH = otherDotSize.height;

    CGFloat mainWidth = currentDotSizeW + (_numberOfPages - 1) * (otherDotSizeW +_controlSpacing);

 if (self.frame.size.width < mainWidth) {
    startX = 0;
 } else {
    startX = (self.frame.size.width - mainWidth) / 2;
 }
 if (self.frame.size.height < otherDotSizeH) {
    startY = 0;
 }  else {
    startY = (self.frame.size.height - otherDotSizeH) / 2;
 }
//动态创建点
 for (int page = 0; page <_numberOfPages; page++) {
    if (page == _currentPage) {
        UIView *currPointView = [[UIView alloc]initWithFrame:CGRectMake(startX, startY, currentDotSizeW, currentDotSizeH)];
        currPointView.layer.cornerRadius = currentDotSizeH / 2;
        currPointView.tag = page + 1000;
        currPointView.backgroundColor = _currentColor;
        currPointView.userInteractionEnabled = YES;
        [self addSubview:currPointView];
        startX = CGRectGetMaxX(currPointView.frame) +_controlSpacing;
    } else {
        UIView *otherPointView = [[UIView alloc]initWithFrame:CGRectMake(startX, startY, otherDotSizeW, otherDotSizeH)];
        otherPointView.backgroundColor = _otherColor;
        otherPointView.tag = page + 1000;
        otherPointView.layer.cornerRadius = otherDotSizeH/2;
        otherPointView.userInteractionEnabled = YES;
        
        [self addSubview:otherPointView];
        startX = CGRectGetMaxX(otherPointView.frame) + _controlSpacing;
    }
  }
 }

- (void)exchangeCurrentView:(NSInteger)old newP:(NSInteger)newPoint {
    UIView *oldSelect = [self viewWithTag:1000+old];
    CGRect mpSelect = oldSelect.frame;

    UIView *newSeltect = [self viewWithTag:1000+newPoint];
    CGRect newTemp = newSeltect.frame;

    oldSelect.backgroundColor = _otherColor;
    newSeltect.backgroundColor = _currentColor;

   [UIView animateWithDuration:0.3 animations:^{
    CGFloat lx = mpSelect.origin.x;
    CGFloat pageW = self.controlSize.width;
    CGFloat pageH = self.controlSize.height;
    if(newPoint < old)
        lx += pageW;
    oldSelect.frame = CGRectMake(lx, mpSelect.origin.y, pageW, pageH);
    
    CGFloat mx = newTemp.origin.x;
    if(newPoint > old)
        mx -= pageW;
    newSeltect.frame = CGRectMake(mx, newTemp.origin.y, pageW*2, pageH);
    
    // 左边的时候到右边 越过点击
    if (newPoint-old>1) {
        for (NSInteger t = old+1; t < newPoint; t++) {
            UIView *ms = [self viewWithTag:1000+t];
            ms.frame = CGRectMake(ms.frame.origin.x-pageW, ms.frame.origin.y, pageW, pageH);
        }
    }
    // 右边选中到左边的时候 越过点击
    if (newPoint-old<-1) {
        for (NSInteger t = newPoint+1; t < old; t++) {
            UIView *ms = [self viewWithTag:1000+t];
            ms.frame = CGRectMake(ms.frame.origin.x+pageW, ms.frame.origin.y, pageW, pageH);
        }
    }
  }];
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容