iOS-自定义 UIPageControl

57 篇文章 1 订阅

现实需求中,当系统自导的 pageControl 无法满足我们的时,需要自己定义一个。通过查看系统自到的UIPageControl 文档,模仿了一个对于个人来说比较灵活的一个 PageControl。


图片示例:

这里写图片描述


解析:系统自带的 UIPageControl,继承自 UIControl

  • 缺点1:无法改变点的大小和样式
  • 缺点2:无法改变点之间的间距

系统UIPageControl代码:
@available(iOS 2.0, *)
open class UIPageControl : UIControl {

    open var numberOfPages: Int // default is 0
    open var currentPage: Int // default is 0. value pinned to 0..numberOfPages-1
    open var hidesForSinglePage: Bool // hide the the indicator if there is only one page. default is NO
    open var defersCurrentPageDisplay: Bool // if set, clicking to a new page won't update the currently displayed page until -updateCurrentPageDisplay is called. default is NO
    open func updateCurrentPageDisplay() // update page display to match the currentPage. ignored if defersCurrentPageDisplay is NO. setting the page value directly will update immediately
    open func size(forNumberOfPages pageCount: Int) -> CGSize // returns minimum size required to display dots for given page count. can be used to size control if page count could change
    @available(iOS 6.0, *)
    open var pageIndicatorTintColor: UIColor?
    @available(iOS 6.0, *)
    open var currentPageIndicatorTintColor: UIColor?
}

自定义 PageControl
主要代码:
class XMPageControl: UIControl {

    // 总页数
    open var numberOfPages: Int = 0
    // 当前所在页数
    open var currentPage: Int = 0 { // default is 0. value pinned to 0..numberOfPages-1
        didSet {
            subviews.filter({ $0.tag == 1000+currentPage }).first?.backgroundColor = currentPageIndicatorTintColor
            subviews.filter({ $0.tag != 1000+currentPage}).forEach({ $0.backgroundColor = pageIndicatorTintColor })
        }
    }
    // 点的间距 默认为 8
    open var distance: CGFloat = 8 // 间距 default is 8
    // 当前页点的颜色
    open var currentPageIndicatorTintColor: UIColor = UIColor.white // default is white
    // 其他点的颜色
    open var pageIndicatorTintColor: UIColor = UIColor.white.withAlphaComponent(0.6) // default is white alpha = 0.6
    // 点的大小
    open var dotSize: CGSize = CGSize(width: 7, height: 7) // default is 7
    // 当有一个点的时候 是否隐藏 默认 false
    open var hidesForSinglePage: Bool = false // hide the the indicator if there is only one page. default is false
    // 是否圆角
    open var isCornerRadius: Bool = true // 是否圆角 default true

    // 布局视图
    override func layoutSubviews() {
        super.layoutSubviews()

        let firstDotOriginX: CGFloat = (frame.size.width - CGFloat(numberOfPages)*dotSize.width - distance * CGFloat(numberOfPages-1))*0.5
        let firstDotOriginY: CGFloat = (frame.size.height - dotSize.height)*0.5

        for i in 0..<numberOfPages {
            // 判断当只有一个点时 是否隐藏
            if numberOfPages <= 1 && hidesForSinglePage {
                return
            }
            let dot = UIView(frame: CGRect(x: firstDotOriginX + CGFloat(i)*(distance+dotSize.width), y: firstDotOriginY, width: dotSize.width, height: dotSize.height))

            if i == currentPage {
                dot.backgroundColor = currentPageIndicatorTintColor
            } else {
                dot.backgroundColor = pageIndicatorTintColor
            }

            dot.tag = 1000 + i
            // 是否切圆角
            dot.layer.cornerRadius = isCornerRadius ? dotSize.height*0.5 : 0
            self.addSubview(dot)

        }
    }
}

Demo 传送门:

XMPageControl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
最新SWIFT 4.0版自定义PageControl,椭圆,空心圆,图片点 刚开始做swift项目,可用资源少而且每个swift版本变化太大,以前的都不能拿来直接用,现在我参考一个object-C的PageControl自己做了一个swift版的, 参考OC资源链接:https://github.com/hackxhj/EllipsePageControl, 非常感谢原作者。 本项目在原OC的功能基础上进行的改进,增加了自定义点的宽度,点的layer,不是当前点的图片等功能 基本能满足大部分的需求,写的很简单,大家一看就懂,欢迎大家使用 由于水平有限,项目中有改进之处忘各位大神给与指点,以求不断完善 本人联系方式:wei287030375@sina.com 如果觉得还可以的话给个star吧,也是对以后进行创作的一种鼓励,谢谢 效果图: image 以下是部分代码: class WEIPageControl: UIControl { var localNumberOfPages = NSInteger()//分页数量 var localCurrentPage = NSInteger()//当前点所在下标 var localPointSize = CGSize()//点的大小 var localPointSpace = CGFloat()//点之间的间距 var localOtherColor = UIColor()//未选中点的颜色 var localCurrentColor = UIColor()//当前点的颜色 var localOtherImage: UIImage?//未选中点的图片 var localCurrentImage: UIImage?//当前点的图片 var localIsSquare = Bool()//是否是方形点 var localCurrentWidthMultiple = CGFloat()//当前选中点宽度与未选中点的宽度的倍数 var localOtherBorderColor: UIColor?//未选中点的layerColor var localOtherBorderWidth: CGFloat?//未选中点的layer宽度 var localCurrentBorderColor: UIColor?//未选中点的layerColor var localCurrentBorderWidth: CGFloat?//未选中点的layer宽度 var clickIndex: ((_ result: NSInteger?) -> ())? 在ViewController中使用 //方形点举例 class ViewController: UIViewController, UIScrollViewDelegate { pageC pageControl3.frame = CGRect.init(x: left, y: scrollView3.frame.maxY, width: width, height: 20) pageControl3.numberOfPages = pageCount//总页数 pageControl3.isSquare = true//设置为方型点 pageControl3.currentWidthMultiple = 2.5//当前点的宽度为其他点的2.5倍 pageControl3.currentColor = UIColor.red pageControl3.otherColor = UIColor.blue pageControl3.pointSize = CGSize.init(width: 14, height: 6)//方点的size pageControl3.clickPoint { (index) in//方点的点击事件 self.scrollView3.setContentOffset(CGPoint.init(x: width * CGFloat(index!), y: 0), animated: true) } self.view.addSubview(pageControl3) 代码用起来就是这么简单,欢迎大家使用, 本项目github地址:https://github.com/wei287030375/WEIPageControl
本Demo使用UICollectionView实现自动无限轮播功能。 主要功能: 1.实现自动轮播,可修改轮播的时间 2.轮播图片可以来自本地,也可来自网络,通过单独的方法进行设置即可。对于加载网络图片时,Demo中使用了YYWebImage,也可自行替换成SDWebImage。 3.重写了和系统UIPageControl一样的功能,可用图片代替PageControl上的点点,也可自定义其颜色以及切换动画。 使用方法:使用方法比较简单。 /** * 加载本地图片Banner */ - (void)setupLocalBannerImageView { NSArray *array = @[@"1.png", @"2.png", @"3.png", @"4.png", @"5.png"]; FFBannerView *bannerVew = [FFBannerView bannerViewWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200) locationImageArray:array]; bannerVew.timeInterval = 2.0; [self.view addSubview:bannerVew]; } /** * 加载网络图片Banner */ - (void)setupNetWorkBannerImageView { NSArray *array = @[@"http://i3.download.fd.pchome.net/t_960x600/g1/M00/07/09/oYYBAFMv8q2IQHunACi90oB0OHIAABbUQAAXO4AKL3q706.jpg", @"http://images.weiphone.net/attachments/photo/Day_120308/118871_91f6133116504086ed1b82e0eb951.jpg", @"http://benyouhuifile.it168.com/forum/macos/attachments/month_1104/110425215921926a173e0f728e.jpg", @"http://benyouhuifile.it168.com/forum/macos/attachments/month_1104/1104241737046031b3a754f783.jpg"]; FFBannerView *bannerVew = [FFBannerView bannerViewWithFrame:CGRectMake(0, 250, [UIScreen mainScreen].bounds.size.width, 200) netWorkImageArray:array placeHolderImage:nil]; bannerVew.timeInterval = 2.0; bannerVew.pageControlStyle = FFPageControlStyleMiddle; bannerVew.delegate = self; [self.view addSubview:bannerVew]; } 以上方式即可简单使用,如需自定义PageControl也可继承FFAbstractDotView,做些基本的设置即可。 gitHub下载地址:喜欢的朋友请给个星呗! 欢迎各位一起来讨论,有问题请发邮箱270452746@qq.com或者直接加我QQ:270452746进行讨论。谢谢!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值