美文网首页
自定义pageControl

自定义pageControl

作者: 有草木青青 | 来源:发表于2017-02-22 19:27 被阅读113次

    由于工程里需求要用到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;
    
    /**
     创建方法
    
     @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
    
    

    相关文章

      网友评论

          本文标题:自定义pageControl

          本文链接:https://www.haomeiwen.com/subject/jmwmwttx.html