由于工程里需求要用到pageControl自定義圖片,用KVC設置圖片會有問題哪痰,間距會變的特別大遂赠,以下是我自己的一些想法
//
// SACustomPageControl.h
// xxx
//
// Created by xxx on 17/2/9.
// Copyright ? 2017年 xxxx科技有限公司. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SACustomPageControl : UIPageControl
/**
* 如果直接使用init初始化、可以手動定義以下屬性
* 其中pageSize為空則跟隨圖片size
*/
/*! 高亮圖片 */
@property (nonatomic, strong) UIImage * currentImage;
/*! 默認圖片 */
@property (nonatomic, strong) UIImage * defaultImage;
/*! 圖標大小 */
@property (nonatomic, assign) CGSize pageSize;
/**
創(chuàng)建方法
@param frame 展示的范圍大小frame
@param currentImage 當前的點顯示的圖片
@param defaultImage 默認的點顯示的圖片
@param pageSize 點的大小
@return self
*/
-(instancetype)initWithFrame:(CGRect)frame
currentImage:(UIImage *)currentImage
andDefaultImage:(UIImage *)defaultImage
pageSize:(CGSize)pageSize;
@end
//
// SACustomPageControl.m
// xx
//
// Created by xxx on 17/2/9.
// Copyright ? 2017年 xxxx科技有限公司. All rights reserved.
//
#import "SACustomPageControl.h"
#import "SAViewHeader.h"
@interface SACustomPageControl()
@property (nonatomic) CGSize size;
@end
@implementation SACustomPageControl
- (instancetype)initWithFrame:(CGRect)frame
currentImage:(UIImage *)currentImage
andDefaultImage:(UIImage *)defaultImage
pageSize:(CGSize)pageSize {
self = [super initWithFrame:frame];
self.currentImage = currentImage;
self.defaultImage = defaultImage;
self.pageSize = pageSize;
return self;
}
- (instancetype)init {
self = [super init];
if (self) {
}
return self;
}
- (void)setUpDots {
if (self.currentImage && self.defaultImage) {
self.size = self.currentImage.size;
}else {
self.size = CGSizeMake(7 * kSACardWidthRatio(), 7 * kSACardHeightRatio());
}
if (self.pageSize.height && self.pageSize.width) {
self.size =self.pageSize;
}
kSAWeakSelf(weakSelf);
[self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj setFrame:CGRectMake(obj.frame.origin.x, obj.frame.origin.y, self.size.width, self.size.width)];
if ([obj.subviews count] == 0) {
UIImageView * view = [[UIImageView alloc]initWithFrame:obj.bounds];
[obj addSubview:view];
};
UIImageView * view = obj.subviews[0];
if (idx == weakSelf.currentPage) {
if (weakSelf.currentImage) {
view.image = weakSelf.currentImage;
obj.backgroundColor = [UIColor clearColor];
}else {
view.image = nil;
obj.backgroundColor = weakSelf.currentPageIndicatorTintColor;
}
}else if (weakSelf.defaultImage) {
view.image = weakSelf.defaultImage;
obj.backgroundColor = [UIColor clearColor];
}else {
view.image = nil;
obj.backgroundColor = weakSelf.pageIndicatorTintColor;
}
}];
}
- (void)setCurrentPage:(NSInteger)page {
[super setCurrentPage:page];
[self setUpDots];
}
@end