自定义UIPageControl圆点图片

作者: kirito_song | 来源:发表于2016-06-01 16:20 被阅读4478次

正常来讲UIPageControl是不能显示图片的

 [UIColorcolorWithPatternImage:[UIImageimageNamed:@"title.png"]]; 

这个方法可以是可以、但是有时也会出现些许误差、然后很容易被UI找事~例如:

屏幕快照 2016-06-01 下午3.48.11.png

于是只好自定义一个UIPageControl
网上有很多自定义的帖子、拿来借鉴了一下、核心代码如下:

首先、颠覆setCurrentPage方法、让每次修改page的时候设置圆点:

-(void) setCurrentPage:(NSInteger)page
{
    [super setCurrentPage:page];

    [self setUpDots];
}

然后、把control上每个圆点上都添加一个imageView、并且根据情况设置图片

for (int i=0; i<[self.subviews count]; i++) {
   //圆点
    UIView* dot = [self.subviews objectAtIndex:i];
  //添加imageView
    if ([dot.subviews count] == 0) {
        UIImageView * view = [[UIImageView alloc]initWithFrame:dot.bounds];
        [dot addSubview:view];
    };

//配置imageView
    UIImageView * view = dot.subviews[0];
    
    if (i==self.currentPage) {
        view.image=self.currentImage;
        dot.backgroundColor = [UIColor clearColor];
    }else {
        view.image=self.defaultImage;
        dot.backgroundColor = [UIColor clearColor];
    }
}

然后、就没啥技术含量了。大概~

屏幕快照 2016-06-01 下午4.09.01.png

然后、自己又在项目里进行了一些封装。添加了一些基本功能

屏幕快照 2016-06-01 下午4.10.33.png

顺手做了个小Demo、有需要可以自取。下载链接

相关文章

网友评论

  • 深圳阳光:我认为你的
    [dot setFrame:CGRectMake(dot.frame.origin.x, dot.frame.origin.y, self.size.width, self.size.width)];应改为
    [dot setFrame:CGRectMake(dot.frame.origin.x, dot.frame.origin.y, self.size.width, self.size.height)];
    因为有些图片是要做成矩形的图标,不一定是宽高相同

    kirito_song:恩对的
  • LeeOuf:这种方法只能修改size,不能修改origin

本文标题:自定义UIPageControl圆点图片

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