iOS --自定义 UIPageControl

最近看时,看到一个不错的自定义UIPageControl ,小兵自己也仿写了下,自定义UIPageControl,一方面系统自带的实在太多不便了,在一方面就当练习了;

iOS --自定义 UIPageControl_第1张图片
徐老茂自定义的UIPageControl.png
小兵自定义UIPagecontrol.gif

这个是自定义的UIPageControl 头文件

#import 

@interface MyPageControl : UIView

typedef enum:NSInteger
{
    /**
     默认类型:圆形
     */
    Q_PageControlStyleDefaoult = 0,
    /**
     正方形
     */
    Q_PageControlStyleSquare,

}Q_PageControlStyle;

@property(nonatomic,assign) NSInteger Q_numberPags;

@property(nonatomic,assign) NSInteger Q_currentPag;

@property(nonatomic,strong) UIColor *Q_selectionColor;

@property(nonatomic,strong) UIColor *Q_backageColor;

@property(nonatomic,assign) Q_PageControlStyle Q_pageStyle;


- (id)initWithFrame:(CGRect)frame pageStyle:(Q_PageControlStyle)pageStyle;


@end

自定义.m文件


#import "MyPageControl.h"
#define arcColor (arc4random() % 255/256.0)


@implementation MyPageControl

- (id)initWithFrame:(CGRect)frame pageStyle:(Q_PageControlStyle)pageStyle
{
    if(self = [super initWithFrame:frame])
    {

        /**
         *  默认设置
         */
        _Q_backageColor = [UIColor darkTextColor];
        _Q_selectionColor = [UIColor whiteColor];
        _Q_pageStyle = pageStyle;
        _Q_currentPag = 0;
        
    }
    return self;
}
/**
 *  设置MyPageView
 *
 *  @param Q_numberPags 
 */
- (void)setQ_numberPags:(NSInteger)Q_numberPags
{
    if(_Q_numberPags != Q_numberPags)
    {
        _Q_numberPags = Q_numberPags;
        
        CGFloat marger = 10;
        NSLog(@"%f--",self.frame.size.width);
        CGFloat width = self.frame.size.width - (Q_numberPags- 1) * marger;
        CGFloat pointWidth = width/Q_numberPags;
        
        for(int i=0;i
 */
- (void)setQ_selectionColor:(UIColor *)Q_selectionColor
{

    
if(_Q_selectionColor != Q_selectionColor)
{
    _Q_selectionColor = Q_selectionColor;
    if(self.subviews.count)
    {
        UIView *aimg = self.subviews[_Q_currentPag];
        aimg.backgroundColor = _Q_selectionColor;
    }
}
    
}

/**
 *  设置pag的backColor 
 *
 *  @param backgroundColor <#backgroundColor description#>
 */
- (void)setBackgroundColor:(UIColor *)backgroundColor
{

    
     
    _Q_backageColor = backgroundColor;
    if(self.subviews.count != 0)
    {
        for(UIView *aimg in self.subviews)
        {
            aimg.backgroundColor = _Q_backageColor;
        }
        UIView *bImg = self.subviews[_Q_currentPag];
        bImg.backgroundColor = _Q_selectionColor;
    }
    
}


在UIViewController中使用

- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    
   
    
    scrollView  = [[UIScrollView alloc] init];
    scrollView.frame = CGRectMake(0, 0, 375, 500);
    scrollView.backgroundColor = [UIColor greenColor];
    scrollView.contentSize = CGSizeMake(375*5, 500);
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    
    for(int i=0;i<5;i++)
    {
        UIImageView *imageVeiw = [[UIImageView alloc] init];
        imageVeiw.image = [UIImage imageNamed:[NSString stringWithFormat:@"a%d",i]];
        imageVeiw.backgroundColor = [UIColor colorWithRed:arcColor green:arcColor blue:arcColor alpha:1];
        imageVeiw.frame = CGRectMake(375*i, 0, 375, 450);
        [scrollView addSubview:imageVeiw];
    }
    [self.view addSubview:scrollView];
   
  
    

    myPag = [[MyPageControl alloc] initWithFrame:CGRectMake(130,600,100, 0) pageStyle:Q_PageControlStyleSquare];
    myPag.backgroundColor = [UIColor redColor];
    myPag.Q_backageColor = [UIColor cyanColor];
    myPag.Q_selectionColor = [UIColor redColor];
    myPag.Q_numberPags = 5;

   
    
    [self.view addSubview:myPag];
}  
- (void)scrollViewDidScroll:(UIScrollView *)scrollVi
{
    CGFloat size = scrollView.contentOffset.x;

    NSInteger index = (size/scrollView.frame.size.width) + 0.5;
        NSLog(@"%f-%ld",size,index);
    myPag.Q_currentPag = index;
    
}

随手点个喜欢吧~

关注我

QQ--iOS 交流群:107548668

你可能感兴趣的:(iOS --自定义 UIPageControl)