使用IBDesignable&amp ;;制作自定义UIControl。 IBInspectable

时间:2017-03-14 16:32:45

标签: swift uipagecontrol uicontrol

我目前正试图制作自己的PageControl版本,就像UIPageControl一样。但是,我从来没有做过这样的事情,我一直坚持如何继续/我应该做些什么才能让某些事情真正发挥作用。

我想要做的是使用PageControl更改interface builder内部点的某些内容,但我不知道该怎么做。

我遵循了this教程,然后开始更改内容,以便根据我的愿望量身定制,但它不起作用(显然我猜)

这就是我班上现在的样子

@IBDesignable
class BoardingPageControl: UIControl {

    @IBInspectable var dotSize: CGFloat! = 7 { didSet { setupView() } }
    @IBInspectable var borderWidth: CGFloat! = 0 { didSet { setupView() } }
    @IBInspectable var borderColor: UIColor! = UIColor.clear { didSet { setupView() } }
    @IBInspectable var dotCount: Int! = 3 { didSet { setupView() } }
    @IBInspectable var dotColor: UIColor! = UIColor.blue { didSet { setupView() } }
    @IBInspectable var dotSelectedColor: UIColor! = UIColor.black { didSet { setupView() } }
    @IBInspectable var dotSelectedBorder: Bool! = false { didSet { setupView() } }

    let DOT_PADDING = 10
    var selectedView = UIView()

    var dots = [UIView]() {
        didSet {
            setupDots()
        }
    }

    var selectedDot: Int = 0 {
        didSet {
            displayNewSelectedIndex()
        }
    }

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

        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupView()
    }

    func setupView() {
        let frameWidth = (dotSize * CGFloat(dotCount)) + CGFloat((DOT_PADDING * dotCount))
        self.frame = CGRect(x: self.frame.minX, y: self.frame.minY,
                            width: frameWidth, height: dotSize)
        backgroundColor = UIColor.clear

        setupDots()
    }

    func setupDots() {
        for dot in dots {
            dot.removeFromSuperview()
        }

        dots.removeAll(keepingCapacity: true)

        for index in 0...dotCount {
            let dot = UIView()
            dot.layer.cornerRadius = dotSize / 2
            dot.clipsToBounds = true

            let x = CGFloat(CGFloat(index) * dotSize + CGFloat(index * DOT_PADDING))
            dot.frame = CGRect(x: x, y: 0, width: dotSize, height: dotSize)

            dot.backgroundColor = dotColor
            dot.layer.borderColor = borderColor.cgColor
            dot.layer.borderWidth = borderWidth
            dot.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)

            self.addSubview(dot)
            dots.append(dot)
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        selectedView.backgroundColor = dotSelectedColor
        selectedView.layer.cornerRadius = selectedView.frame.height / 2

        if !dotSelectedBorder {
            selectedView.layer.borderWidth = 0
        }
        for index in 0...dots.count - 1 {
            var dot = dots[index]

            dot = selectedView
        }
    }

    func displayNewSelectedIndex() {
        let dot = dots[selectedDot]
        self.selectedView.frame = dot.frame
    }

}

此时正在考虑在interface builder处输入的值(可能在设置之前尝试使用它们?),因为它们很重要。

我希望有人可以帮助我/指导我如何做到这一点。

0 个答案:

没有答案