customize UIPageControl dots

不羁的心 提交于 2019-12-11 07:01:16

问题


I'm currently trying to customize the UIPageControl so it will fit my needs. However I seem to be having a problem when implementing some logic in the draw

What I want to do is to be able to user IBInspectable variables to draw out the UIPageControl however those seem to still be nil when the draw method is being called and when I try to implement my logic in the awakeFromNib for instance it won't work.

What I did so far is the following

class BoardingPager: UIPageControl {
    @IBInspectable var size: CGSize!
    @IBInspectable var borderColor: UIColor!
    @IBInspectable var borderWidth: CGFloat! = 1

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.pageIndicatorTintColor = UIColor.clear
    }

    override func draw(_ rect: CGRect) {
        super.draw(rect)
        setDots()
    }

    func setDots() {
        for i in (0..<self.numberOfPages) {
            let dot = self.subviews[i]
            if size != nil {
                let dotFrame = CGRect(x: size.width/2, y: size.height/2, width: size.width, height: size.height)
                dot.frame = dotFrame
            }

            if i != self.currentPage {
                dot.layer.cornerRadius = dot.frame.size.height / 2
                dot.layer.borderColor = borderColor.cgColor
                dot.layer.borderWidth = borderWidth
            }
        }

    }
}

Another problem I'm facing is that I want to remove/add a border when the current page changes.

I'm hoping someone will be able to help me out

来源:https://stackoverflow.com/questions/42783598/customize-uipagecontrol-dots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!