自定義pageControl

由于工程里需求要用到pageControl自定義圖片,用KVC設(shè)置圖片會有問題,間距會變的特別大,以下是我自己的一些想法

//
//  SACustomPageControl.h
//  xxx
//
//  Created by xxx on 17/2/9.
//  Copyright ? 2017年 xxxx科技有限公司. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SACustomPageControl : UIPageControl

/**
 *  如果直接使用init初始化、可以手動(dòng)定義以下屬性
 *  其中pageSize為空則跟隨圖片size
 */
/*! 高亮圖片 */
@property (nonatomic, strong) UIImage * currentImage;

/*! 默認(rèn)圖片 */
@property (nonatomic, strong) UIImage * defaultImage;

/*! 圖標(biāo)大小 */
@property (nonatomic, assign) CGSize pageSize;

/**
 創(chuàng)建方法

 @param frame 展示的范圍大小frame
 @param currentImage 當(dāng)前的點(diǎn)顯示的圖片
 @param defaultImage 默認(rèn)的點(diǎn)顯示的圖片
 @param pageSize 點(diǎn)的大小
 @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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容