iOS14 自定义UIPageControl

iOS14 更新也有一段时间了,在iOS14以前,很多人都通过重写UIPageControl来自定义自己的UIPageControl
但是在iOS14到来时,就都不好使了。
由于公司项目之前也是用的重写,现在亦不可用,在使用原生UIPageControl样式先顶着用了一段时间,最近业务上的需求总算是结束了,空闲时间就自己撸了一个HBPageControl,也算是提供一种思路给大家。可以根据各自的业务要求做不同的样式。
样式如下。

图像.gif

安装

pod 'HBPageControl'

Demo地址

https://github.com/GitHubYhb/HBPageControl

可修改样式

dotColor:UIColor  点的颜色
dotSelectedColor:UIColor  选中点颜色
spacing:CGFloat  间距
dotWidth:CGFloat  点宽度
dotHeight:CGFloat  点高度

实现很简单

import UIKit
public class HBPageControl: UIView {

    public var dotColor:UIColor
    public var dotSelectedColor:UIColor
    public var spacing:CGFloat
    public var dotWidth:CGFloat
    public var dotHeight:CGFloat
    public var numberOfPages:Int {
        didSet {
            resetDots()
        }
    }
    
    public var currentPage:Int{
        didSet{
            if currentPage > numberOfPages-1 {
                fatalError("currentPage can not bigger than numberOfPages")
            }
            if dots.count > 0 {
                let preView = dots[oldValue]
                preView.backgroundColor = dotColor
                let preWidth = widths[oldValue]
                preWidth.constant = dotWidth
                
                let view = dots[currentPage]
                view.backgroundColor = dotSelectedColor
                let width = widths[currentPage]
                width.constant = dotWidth + 5

                UIView.animate(withDuration: 0.2, delay: 0, options: .curveLinear) {
                    self.layoutIfNeeded()
                } completion: { finish in
                    
                }
            }
        }
    }
    private var bgView:UIView!
    private var dots = [UIView]()
    private var widths = [NSLayoutConstraint]()
    
    public override init(frame: CGRect) {
        
        self.dotColor = UIColor.init(red: 1, green: 1, blue: 1, alpha: 0.5)
        self.dotSelectedColor = UIColor.init(red: 1, green: 1, blue: 1, alpha: 1)
        self.spacing = 5
        self.dotWidth = 10
        self.dotHeight = 10
        self.numberOfPages = 5
        self.currentPage = 0
        super.init(frame: frame)
        setupDots()
    }
    
    public override func layoutSubviews() {
        superview?.layoutSubviews()
        resetDots()
    }
    
    private func resetDots(){
        for v in dots {
            v.removeFromSuperview()
        }
        bgView.removeFromSuperview()
        dots.removeAll()
        widths.removeAll()
        currentPage = 0
        setupDots()
    }
    
    private func setupDots()  {
        if numberOfPages <= 0 {
            return
        }
        let viewWidth = CGFloat(numberOfPages)*(dotWidth+spacing)-spacing
        bgView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: Int(viewWidth), height: Int(frame.size.height)))
        bgView.center = CGPoint.init(x: frame.midX, y: frame.size.height/2)
        addSubview(bgView)
        
        for i in 0..<numberOfPages {
            let v = UIView.init()
            v.layer.cornerRadius = dotHeight / 2
            v.translatesAutoresizingMaskIntoConstraints = false
            var dWidth:CGFloat = dotWidth
            if i == currentPage {
                dWidth = dotWidth + 5
                v.backgroundColor = dotSelectedColor
            }else{
                dWidth = dotWidth
                v.backgroundColor = dotColor
            }
            bgView.addSubview(v)
            dots.append(v)
            
            //宽度约束
            let width:NSLayoutConstraint = NSLayoutConstraint(item: v, attribute: .width, relatedBy:.equal, toItem:nil, attribute: .notAnAttribute, multiplier:0.0, constant:dWidth)
            v.addConstraint(width)//自己添加约束
            widths.append(width)
            
            //高度约束
            let height:NSLayoutConstraint = NSLayoutConstraint(item: v, attribute: .height, relatedBy:.equal, toItem:nil, attribute: .notAnAttribute, multiplier:0.0, constant:dotHeight)
            v.addConstraint(height)//自己添加约束
            
            var pre = bgView
            var leftSpacing:CGFloat = 0
            if i > 0 {
                pre = dots[i-1]
                leftSpacing = spacing
                //右边约束
                let right:NSLayoutConstraint = NSLayoutConstraint(item: v, attribute: .left, relatedBy:.equal, toItem:pre, attribute:.right, multiplier:1.0, constant: leftSpacing)
                
                v.superview!.addConstraint(right)//父控件添加约束
            }else{
                //右边约束
                let right:NSLayoutConstraint = NSLayoutConstraint(item: v, attribute: .left, relatedBy:.equal, toItem:pre, attribute:.left, multiplier:1.0, constant: leftSpacing)
                
                v.superview!.addConstraint(right)//父控件添加约束
            }
            //下边约束
            let centerY:NSLayoutConstraint = NSLayoutConstraint(item: v,attribute: .centerY, relatedBy:.equal, toItem: pre, attribute:.centerY, multiplier:1.0, constant: 0)
            
            v.superview!.addConstraint(centerY)//父控件添加约束
            
        }
    }
 
    required init?(coder: NSCoder) {
        fatalError("HBPageControl Error")
    }
    

}

结束

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 又是一年苹果发布会,苹果更新了新版本的系统iOS14。就我个人来说的话系统耗电有优化,小程序类似部件化APP体验也...
    尼古拉斯佩思阅读 4,102评论 0 9
  • 开发中遇到的问题, iOS14已经发布,记录一下开发中遇到的问题.不定时更新,欢迎大家评论补充. iOS14适配总...
    香橙柚子阅读 8,674评论 20 44
  • 前言 iOS14发布已经有一段时间了,更新完之后最让我好奇的就是这个Widget功能。从用户使用到开发者开发上与i...
    RocketsChen阅读 10,218评论 3 19
  • Widget 是 iOS14 的新特性,是一个桌面小插件,它的 UI 需要使用到 SwiftUI, 所以想要写 W...
    supersmalltalk阅读 2,226评论 2 4
  • UIDatePicker 在 iOS 14 开始,UIDatePicker 默认样式为: 而在 iOS14 之前的...
    王家小雷阅读 997评论 0 4