美文网首页
自定义UIPageControl

自定义UIPageControl

作者: 龙之天下 | 来源:发表于2021-04-25 17:33 被阅读0次

1、自定义UIPageControl

class SMPageControl: UIView {
    private let scrollView = UIScrollView()
    private let space: CGFloat = 8

    /// numberOfPages--数量尽量小
    open var numberOfPages: Int = 0 {
        didSet {
            self.setNeedsLayout()
            self.layoutIfNeeded()
        }
    }
    
    open var currentPage: Int = 0 {
        didSet {
            self.setNeedsLayout()
            self.layoutIfNeeded()
        }
    }
    
    open var currentPageImage = UIImage.imageWithColor(UIColor(hex: "1F59EE")!,size: CGSize(width: 14, height: 4))
    open var pageImage = UIImage.imageWithColor(UIColor.white.withAlphaComponent(0.5),size: CGSize(width: 4, height: 4))

    override init(frame: CGRect) {
        super.init(frame: frame)
        let button = createButton()
        self.addSubview(scrollView)
        scrollView.addSubview(button)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        scrollView.frame = CGRect(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height)
        
        let subviews = getButtons(scrollView)
        
        let maxCount = subviews.count
        let count = numberOfPages - maxCount
        guard count >= 0 else {
            return
        }
        for _ in 0..<count {
            scrollView.addSubview(createButton())
        }
        
        let size = pageImage.size
        let selectedSize = currentPageImage.size
        let margin = space
        let contentWidth = CGFloat(maxCount - 1)*(size.width + margin) + selectedSize.width
        var originX: CGFloat = self.bounds.size.width - contentWidth
        if originX < 0 {
            originX = 0
        }
        for i in 0..<maxCount {
            if let item = subviews[i] as? UIButton {
                item.isSelected = false
                if i == currentPage {
                    item.isSelected = true
                    item.frame = CGRect(origin: CGPoint(x: originX, y: 0), size: selectedSize)
                }else {
                    item.frame = CGRect(origin: CGPoint(x: originX, y: 0), size: size)
                }
                updateButton(item)
                originX += margin + (i == currentPage ? selectedSize.width : size.width)
            }
        }
    }
    
    private func getButtons(_ subSuper: UIView) -> [UIView] {
       return subSuper.subviews.filter({ (view) -> Bool in
            return (view as? UIButton != nil)
        })
    }
    
    private func updateButton(_ btn: UIButton) {
        if ((btn.image(for: .selected) != currentPageImage) || btn.image(for: .normal) != pageImage) {
            btn.setImage(currentPageImage, for: .selected)
            btn.setImage(pageImage, for: .normal)
        }
    }
    
    private func createButton() -> UIButton {
        let button = UIButton(type: .custom)
        button.setImage(pageImage, for: .normal)
        button.setImage(currentPageImage, for: .selected)
        button.layer.cornerRadius = 2
        button.layer.masksToBounds = true
        return button
    }
}

相关文章

网友评论

      本文标题:自定义UIPageControl

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