轮播图iOS基础

ios 关于UIPageControl 自定义样式定制的正确姿势

2020-10-20  本文已影响0人  缘來諟夢

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/a416863220/article/details/43407517

近来,项目中需要定制UIPageControl样式,目前知道的方式大概如下,这里简单概括:

一、修改颜色,最简单的方式:

// 设置非选中页的圆点颜色
_pageControl.pageIndicatorTintColor = [UIColor redColor];
// 设置选中页的圆点颜色
_pageControl.currentPageIndicatorTintColor = [UIColor blueColor]; 

二、修改静态图标样式,可以采用KVO方式,这种弊端是只能静态显示,无法动态替换,且私有,危险:

[_pageControl setValue:[UIImage imageNamed:@"image1"] forKeyPath:@"_pageImage"]; 
[_pageControl setValue:[UIImage imageNamed:@"image2"] forKeyPath:@"_currentPageImage"];

三、替换系统默认图标,自定义样式:

常规方式便是创建一个类,继承于UIPageControl,重新set方式,这里写下核心代码,也是目前网络上的方式:

-(void) updateDots {            
     for (int i = 0; i < [self.subviews count]; i++)  
{       
    UIImageView *dot = [self.subviews objectAtIndex:i];       
    if (i == self.currentPage) {
       dot.image = activeImage;     
    }
    else 
       dot.image = inactiveImage;   
     
 }

结果是程序崩溃,归咎原因:

这种方式及其危险的,因为[self.subviews objectAtIndex:i] class 为UIView, 而 UIImageView *dot = [self.subviews objectAtIndex:i]; 这句是将一个UIView转换为UIImageView, 故容易crash 掉,特此放弃如上国内方式;

四、完善上面第三种方式,转为安全的替换方式,核心代码如下:

for (UIView *subview in view.subviews) {          
  if ([subview isKindOfClass:[UIImageView class]]) {           
     dot = (UIImageView *)subview;         
      break;       
     }      
  }

采用如上安全方式,可有效防止崩溃,至此完成项目需求,效果如下:

image

五、这里把整个代码放出来,帮助大家少走弯路,具体代码如下:

1、.h文件

#import <UIKit/UIKit.h> 
@interface CustomPageControl : UIPageControl
@property (nonatomic, strong) UIImage *currentImage;
@property (nonatomic, strong) UIImage *inactiveImage; 
@property (nonatomic, assign) CGSize currentImageSize;
@property (nonatomic, assign) CGSize inactiveImageSize;
@end

2、.m文件

#import "CustomPageControl.h" 
@implementation CustomPageControl 
- (instancetype)init{   
 self = [super init];  
  if (self) {      
  self.userInteractionEnabled = NO;   
 }   
 return self;
} 
- (void)setCurrentPage:(NSInteger)currentPage{ 
   [super setCurrentPage:currentPage]; 
    [self updateDots];
} 
 - (void)updateDots{   
 for (int i = 0; i < [self.subviews count]; i++) {    
    UIImageView *dot = [self imageViewForSubview:[self.subviews objectAtIndex:i] currPage:i];      
  if (i == self.currentPage){      
      dot.image = self.currentImage;     
       dot.size = self.currentImageSize;     
  }else{           
 dot.image = self.inactiveImage;        
   dot.size = self.inactiveImageSize;     
   } 
  }
}
- (UIImageView *)imageViewForSubview:(UIView *)view currPage:(int)currPage{    UIImageView *dot = nil;   
 if ([view isKindOfClass:[UIView class]]) {   
     for (UIView *subview in view.subviews) {   
         if ([subview isKindOfClass:[UIImageView class]]) {       
         dot = (UIImageView *)subview;      
          break;     
       }     
   }              
  if (dot == nil) {       
     dot = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, view.frame.size.width, view.frame.size.height)];              
         [view addSubview:dot];  
      }  
  }else {       
 dot = (UIImageView *)view;   
 }       
 return dot;
}
@end

3、使用方式:

CustomPageControl *pageCtrl = [[CustomPageControl alloc] initWithFrame:CGRectMake(0, 10, mScreenWidth, 5)]; 
[pageCtrl.numberOfPages = cardSwitchViewAry.count;
pageCtrl.currentPage = 0; 
pageCtrl.userInteractionEnabled = NO;
pageCtrl.inactiveImage = [UIImage imageNamed:@"ex_page_1"];
pageCtrl.inactiveImageSize = CGSizeMake(5, 5);
pageCtrl.currentImage = [UIImage imageNamed:@"ex_page_2"];
pageCtrl.currentImageSize = CGSizeMake(10, 5);     
 //去掉系统自带样式
pageCtrl.currentPageIndicatorTintColor = [UIColor clearColor];
pageCtrl.pageIndicatorTintColor = [UIColor clearColor];    

至此,定制UIPageControl的正确姿势已了解,希望对大家有用。

上一篇下一篇

猜你喜欢

热点阅读