Sin descripción

KXCustomPageControl.m 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // KXCustomPageControl.m
  3. // CAISHEN
  4. //
  5. // Created by jikaipeng on 2018/5/22.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KXCustomPageControl.h"
  9. #define pageDistance 8 // 边距
  10. @interface KXCustomPageControl (){
  11. NSInteger pageNumber; // 总的个数
  12. NSInteger currentPageNumber; // 当前的下标
  13. UIImage *currentImage; // 当前图片
  14. UIImage *pageIndicatorImage; // 接下来图片
  15. CGSize currentImageSize; // 当前图片的Size
  16. CGSize pageIndicatorImageSize; // 接下来图片的Size
  17. }
  18. @end
  19. @implementation KXCustomPageControl
  20. -(instancetype)initWithCusPageControl:(CGRect)frame pageNum:(NSInteger)pageNum currentPageIndex:(NSInteger)currentPageIndex currentShowImage:(UIImage *)currentShowImage pageIndicatorShowImage:(UIImage *)pageIndicatorShowImage{
  21. if(self=[super initWithFrame:frame]){
  22. self.frame=frame;
  23. pageNumber=pageNum;
  24. currentPageNumber=currentPageIndex;
  25. currentImage=currentShowImage;
  26. pageIndicatorImage=pageIndicatorShowImage;
  27. currentImageSize=currentImage.size;
  28. pageIndicatorImageSize=pageIndicatorImage.size;
  29. [self createUI]; // 创建对应的UI
  30. }
  31. return self;
  32. }
  33. +(instancetype)cusPageControlWithView:(CGRect)frame pageNum:(NSInteger)pageNum currentPageIndex:(NSInteger)currentPageIndex currentShowImage:(UIImage *)currentShowImage pageIndicatorShowImage:(UIImage *)pageIndicatorShowImage{
  34. return [[self alloc]initWithCusPageControl:frame pageNum:pageNum currentPageIndex:currentPageIndex currentShowImage:currentShowImage pageIndicatorShowImage:pageIndicatorShowImage];
  35. }
  36. #pragma mark 创建对应的UI
  37. -(void)createUI{
  38. for (int i=0;i<pageNumber; i++) { // 创建对应的Button和设置Frame
  39. UIButton *button=[[UIButton alloc]init];
  40. [self addSubview:button];
  41. button.tag=i;
  42. [button addTarget:self action:@selector(clickPageAction:) forControlEvents:UIControlEventTouchUpInside];
  43. }
  44. [self calculateFrame]; // 计算对应的Frame
  45. }
  46. #pragma mark 计算各个视图对应的Frame
  47. -(void)calculateFrame{
  48. CGFloat needMinWidth=(pageNumber-1)*(pageIndicatorImageSize.width+pageDistance)+currentImageSize.width; // 边距和Page的最小宽度
  49. CGFloat beginXValue=self.frame.size.width/2-needMinWidth/2; // 开始的X值
  50. for (int i=0; i<self.subviews.count; i++) {
  51. UIView *view=self.subviews[i];
  52. if ([view isKindOfClass:[UIButton class]]) {
  53. UIButton *button=(UIButton *)view;
  54. if(i==currentPageNumber){ // 当前的点
  55. [button setImage:currentImage forState:UIControlStateNormal]; // 设置对应的UIImage
  56. [button setImage:currentImage forState:UIControlStateHighlighted];
  57. button.frame=CGRectMake(beginXValue+(pageIndicatorImageSize.width+pageDistance)*currentPageNumber, (self.frame.size.height/2-currentImageSize.height/2), currentImageSize.width, currentImageSize.height); // 设置对应的Frame
  58. }
  59. else{ // 接下来的点
  60. CGFloat heightWithButton=self.frame.size.height/2-pageIndicatorImageSize.height/2;
  61. if(i>currentPageNumber){
  62. button.frame=CGRectMake(beginXValue+pageDistance*i+(i-1)*pageIndicatorImageSize.width+currentImageSize.width, heightWithButton, pageIndicatorImageSize.width, pageIndicatorImageSize.height);
  63. }
  64. else{
  65. button.frame=CGRectMake(beginXValue+(pageIndicatorImageSize.width+pageDistance)*i, heightWithButton, pageIndicatorImageSize.width, pageIndicatorImageSize.height);
  66. }
  67. [button setImage:pageIndicatorImage forState:UIControlStateNormal]; // 设置对应的UIImage
  68. [button setImage:pageIndicatorImage forState:UIControlStateHighlighted];
  69. }
  70. }
  71. }
  72. }
  73. #pragma mark 点击了对应的点
  74. -(void)clickPageAction:(UIButton *)sender{
  75. if([self.delegate respondsToSelector:@selector(pageControlView:indexNum:)]){
  76. [self.delegate pageControlView:self indexNum:sender.tag];
  77. }
  78. }
  79. #pragma mark 重写Setting方法
  80. -(void)setIndexNumWithSlide:(NSInteger)indexNumWithSlide{
  81. if (indexNumWithSlide>=self.subviews.count) return;
  82. if (currentPageNumber!=indexNumWithSlide) {
  83. // NSLog(@"移动到的下标:%zi",indexNumWithSlide);
  84. _indexNumWithSlide=indexNumWithSlide;
  85. currentPageNumber=indexNumWithSlide;
  86. [self calculateFrame]; // 重写计算对应的Frame
  87. }
  88. }
  89. @end