Mako Shan

Mako 是一名密码朋克爱好者
这里是我记录生活和成长的地方

联系我的微信号
👏欢迎一起交流学习

自定义UIPageControl Dot样式指定图片

import UIKit

class CustomPageControl: UIPageControl {
    let activeCircle = UIImage(named: "pageDot")!
    let inactiveCircle = UIImage(named: "currentPageDot")!
    let activePlus = UIImage(named: "pageDot")!
    let inactivePlus = UIImage(named: "currentPageDot")!
    
    var starPosition: Int = 0 {
        didSet {
            updateDots()
        }
    }
    var plusPosition = 0
    
    let ratio = CGFloat(0.3)
    let ratio2=CGFloat(1.3)
    
    dynamic override var currentPage: Int {
        didSet {
            updateDots()
        }
    }
    
    override var numberOfPages: Int {
        didSet {
            updateDots()
        }
    }
    
    private func updateDots() {
        for i in 0..<subviews.count {
            let dot = imageViewForSubview(subviews[i] as UIView)
            if i == currentPage {
                switch i {
                case plusPosition:
                    dot.image = activePlus
                default:
                    dot.image = activeCircle
                }
            }
            else {
                switch i {
                case plusPosition:
                    dot.image = inactivePlus
                default:
                    dot.image = inactiveCircle
                }
            }
        }
    }
    
    private func imageViewForSubview(view : UIView) -> UIImageView {
        var dot: UIImageView!
        if view.isKindOfClass(UIView) {
            for subview in view.subviews {
                if subview.isKindOfClass(UIImageView) {
                    dot = subview as? UIImageView
                    break
                }
            }
            if dot == nil {
                dot = UIImageView(frame: CGRectMake(0, 0, CGRectGetWidth(view.frame) * ratio2, CGRectGetHeight(view.frame) * ratio))
                view.addSubview(dot)
            }
        } else {
            dot = view as? UIImageView
        }
        _ = dot.center
        return dot
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        pageIndicatorTintColor = UIColor.clearColor()
        currentPageIndicatorTintColor = UIColor.clearColor()
    }
    
}
0
UIDevice扩展
目录