自定义滚动控件(Pagecontrol)

//
//  MyPageCorol.h
//  lejiahui
//
//  Created by iOS开发 on 16/4/10.
//  Copyright © 2016年 zhongmingwuye. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MyPageCorol : UIView

/**  当前页数 */
@property(nonatomic,assign) NSInteger  correntPage;

/** itmenSize */
@property(nonatomic,assign) CGSize itmeSize;

+ (MyPageCorol *)myPageControlWithSuperView:(UIView *)superView;

- (void)setImage:(UIImage *)image andSelectImage:(UIImage *)slectImage totalNum:(NSInteger)totalNum;

- (void)setColor:(UIColor *)color andSelectColor:(UIColor*)selectColor totalNum:(NSInteger)totalNum;

@end

//
//  MyPageCorol.m
//  lejiahui
//
//  Created by iOS开发 on 16/4/10.
//  Copyright © 2016年 zhongmingwuye. All rights reserved.
//

#import "MyPageCorol.h"

@interface MyPageCorol()
{
    NSMutableArray * _pageItems;
    UIColor * _selectedColor;
    UIColor * _color;
    
    UIImage * _image;
    UIImage * _selectedImage;
    
    BOOL _isImage;
    
    NSInteger _width;
    NSInteger _height;
}

@end

@implementation MyPageCorol

+ (MyPageCorol *)myPageControlWithSuperView:(UIView *)superView{
    MyPageCorol * pl = [[MyPageCorol alloc]init];
    pl.frame = CGRectMake(0, superView.frame.size.height-20, superView.frame.size.width, 20);
    pl.backgroundColor = [UIColor clearColor];
    [superView addSubview:pl];
    return pl;
}

- (instancetype)init{
    if (self=[super init]) {
        self.itmeSize = CGSizeMake(0, 0);
    }
    return self;
}

- (void)setItmeSize:(CGSize)itmeSize{
    _itmeSize = itmeSize;
    _width = itmeSize.width;
    _height = itmeSize.height;
}
//设置图片形式
- (void)setImage:(UIImage *)image andSelectImage:(UIImage *)slectImage totalNum:(NSInteger)totalNum{
    _isImage = YES;
    _image = image;
    _selectedImage = slectImage;
    
    _pageItems = [[NSMutableArray alloc]init];
    NSInteger num = totalNum;
    
    NSInteger height = 10;
    NSInteger width  =20;
    if (self.itmeSize.height!=0&&self.itmeSize.width!=0) {
        height = self.itmeSize.height;
        width = self.itmeSize.width;
    }
    NSInteger sepreate = 5;
    NSInteger totalWidth = num*width+(num-1)*sepreate;
    NSInteger orangex = (self.frame.size.width-totalWidth)/2;
    NSInteger orangey = (self.frame.size.height - height)/2;
    for (int i = 0;i< num; i++) {
        UIImageView * pageView = [[UIImageView alloc]init];
        pageView.frame = CGRectMake(orangex+i*(width+sepreate), orangey, width, height);
        [_pageItems addObject:pageView];
        [self addSubview:pageView];
    }
    self.correntPage = 0;
}

//设置颜色形式
- (void)setColor:(UIColor *)color andSelectColor:(UIColor*)selectColor totalNum:(NSInteger)totalNum{
    _pageItems = [[NSMutableArray alloc]init];
    _color = color;
    _selectedColor = selectColor;

    NSInteger num = totalNum;
    NSInteger height = 5;
    NSInteger width  =20;
    if (self.itmeSize.height!=0&&self.itmeSize.width!=0) {
        height = self.itmeSize.height;
        width = self.itmeSize.width;
    }
    NSInteger sepreate = 5;
    NSInteger totalWidth = num*width+(num-1)*sepreate;
    NSInteger orangex = (self.frame.size.width-totalWidth)/2;
    NSInteger orangey = (self.frame.size.height - height)/2;
    for (int i = 0;i< num; i++) {
        UIView * pageView = [[UIView alloc]init];
        pageView.frame = CGRectMake(orangex+i*(width+sepreate), orangey, width, height);
        [_pageItems addObject:pageView];
        [self addSubview:pageView];
    }
    self.correntPage = 0;
}

- (void)setCorrentPage:(NSInteger)correntPage{
    _correntPage = correntPage;
    for (int i = 0; i<_pageItems.count; i++) {
        if (_isImage) {
            UIImageView * imageView= _pageItems[i];
            imageView.image = _image;
            if (correntPage==i) {
                imageView.image = _selectedImage;
            }
        }else{
            UIView * view = _pageItems[i];
            view.backgroundColor = _color;
            if (correntPage==i) {
                view.backgroundColor = _selectedColor;
            }
        }
    }
}

@end

 

posted @ 2016-04-10 15:11  MakeKeyReuseable  阅读(210)  评论(0编辑  收藏  举报