// // KXCustomPageControl.m // CAISHEN // // Created by jikaipeng on 2018/5/22. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KXCustomPageControl.h" #define pageDistance 8 // 边距 @interface KXCustomPageControl (){ NSInteger pageNumber; // 总的个数 NSInteger currentPageNumber; // 当前的下标 UIImage *currentImage; // 当前图片 UIImage *pageIndicatorImage; // 接下来图片 CGSize currentImageSize; // 当前图片的Size CGSize pageIndicatorImageSize; // 接下来图片的Size } @end @implementation KXCustomPageControl -(instancetype)initWithCusPageControl:(CGRect)frame pageNum:(NSInteger)pageNum currentPageIndex:(NSInteger)currentPageIndex currentShowImage:(UIImage *)currentShowImage pageIndicatorShowImage:(UIImage *)pageIndicatorShowImage{ if(self=[super initWithFrame:frame]){ self.frame=frame; pageNumber=pageNum; currentPageNumber=currentPageIndex; currentImage=currentShowImage; pageIndicatorImage=pageIndicatorShowImage; currentImageSize=currentImage.size; pageIndicatorImageSize=pageIndicatorImage.size; [self createUI]; // 创建对应的UI } return self; } +(instancetype)cusPageControlWithView:(CGRect)frame pageNum:(NSInteger)pageNum currentPageIndex:(NSInteger)currentPageIndex currentShowImage:(UIImage *)currentShowImage pageIndicatorShowImage:(UIImage *)pageIndicatorShowImage{ return [[self alloc]initWithCusPageControl:frame pageNum:pageNum currentPageIndex:currentPageIndex currentShowImage:currentShowImage pageIndicatorShowImage:pageIndicatorShowImage]; } #pragma mark 创建对应的UI -(void)createUI{ for (int i=0;icurrentPageNumber){ button.frame=CGRectMake(beginXValue+pageDistance*i+(i-1)*pageIndicatorImageSize.width+currentImageSize.width, heightWithButton, pageIndicatorImageSize.width, pageIndicatorImageSize.height); } else{ button.frame=CGRectMake(beginXValue+(pageIndicatorImageSize.width+pageDistance)*i, heightWithButton, pageIndicatorImageSize.width, pageIndicatorImageSize.height); } [button setImage:pageIndicatorImage forState:UIControlStateNormal]; // 设置对应的UIImage [button setImage:pageIndicatorImage forState:UIControlStateHighlighted]; } } } } #pragma mark 点击了对应的点 -(void)clickPageAction:(UIButton *)sender{ if([self.delegate respondsToSelector:@selector(pageControlView:indexNum:)]){ [self.delegate pageControlView:self indexNum:sender.tag]; } } #pragma mark 重写Setting方法 -(void)setIndexNumWithSlide:(NSInteger)indexNumWithSlide{ if (indexNumWithSlide>=self.subviews.count) return; if (currentPageNumber!=indexNumWithSlide) { // NSLog(@"移动到的下标:%zi",indexNumWithSlide); _indexNumWithSlide=indexNumWithSlide; currentPageNumber=indexNumWithSlide; [self calculateFrame]; // 重写计算对应的Frame } } @end