美文网首页
ios UIPageControl 点颜色设置的总结

ios UIPageControl 点颜色设置的总结

作者: Treey_Ahao | 来源:发表于2017-03-21 15:48 被阅读0次

    1、写一个继承UIPageControl类

    2、重写UIPageControl的 setCurrentPage方法

    3、修改当前点的颜色

    code: 

    #import

    @interface CommonPageControl : UIPageControl

    {

    UIImage *activeImage;

    UIImage *inactiveImage;

    }

    @end

    #import "CommonPageControl.h"

    @implementation CommonPageControl

    - (id)initWithFrame:(CGRect)frame

    {

    self = [super initWithFrame:frame];

    if (self) {

    // Initialization code

    }

    return self;

    }

    -(void)updateDots{

    for(int i=0;i<[self.subviews count];i++){

    if([(UIView *)[self.subviews objectAtIndex:i] isKindOfClass:[UIView class]]){//目前pageControl控件小点是一个view

    UIView *dot=[self.subviews objectAtIndex:i];

    if(i==self.currentPage){

    dot.backgroundColor=[UIColor whiteColor];

    }

    else{

    dot.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1];

    }

    }

    }

    }

    //重写基类方法

    -(void)setCurrentPage:(NSInteger)currentPage{

    [super setCurrentPage:currentPage];

    [self updateDots];

    }

    @end

    调用:

    CGRect rect;

    rect.origin.x = myScrollView.frame.origin.x;

    rect.origin.y = self.frame.size.height-20;

    rect.size.width = myScrollView.frame.size.width;

    rect.size.height = 20;

    myPagecontrol = [[CommonPageControl alloc] initWithFrame:rect];

    myPagecontrol.userInteractionEnabled=NO;

    [self.view addSubview:myPagecontrol];

    相关文章

      网友评论

          本文标题:ios UIPageControl 点颜色设置的总结

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