Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用IBDesignable+UIView(情节提要)查看绘图,但在应用程序上看不到_Ios_Swift_Storyboard_Core Graphics_Ibdesignable - Fatal编程技术网

Ios 使用IBDesignable+UIView(情节提要)查看绘图,但在应用程序上看不到

Ios 使用IBDesignable+UIView(情节提要)查看绘图,但在应用程序上看不到,ios,swift,storyboard,core-graphics,ibdesignable,Ios,Swift,Storyboard,Core Graphics,Ibdesignable,我试图在应用程序上绘制一些虚线,但它仅在带有IBDesignable的main.storyboard上绘制。当我在iOS模拟器上运行应用程序时,没有显示任何内容。发生了什么事 要绘制的代码: @IBDesignable class AnalogView: UIView { fileprivate let thickHorizontalLayer = CAShapeLayer() fileprivate let thinHorizontalLa

我试图在应用程序上绘制一些虚线,但它仅在带有IBDesignable的main.storyboard上绘制。当我在iOS模拟器上运行应用程序时,没有显示任何内容。发生了什么事

要绘制的代码:

    @IBDesignable
    class AnalogView: UIView {



        fileprivate let thickHorizontalLayer = CAShapeLayer()
        fileprivate let thinHorizontalLayer = CAShapeLayer()

        @IBInspectable var thickYCoord = 50.0
        @IBInspectable var thinYCoord = 52.5

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

            let thickDashesPath = UIBezierPath()
            thickDashesPath.move(to: CGPoint(x: 0, y: thickYCoord)) //left

            thickDashesPath.addLine(to: CGPoint(x: 340, y: thickYCoord)) //right

            //thickHorizontalLayer.frame           = frame
            thickHorizontalLayer.path            = thickDashesPath.cgPath
            thickHorizontalLayer.strokeColor     = UIColor.black.cgColor //dashes color
            thickHorizontalLayer.lineWidth       = 20
            thickHorizontalLayer.lineDashPattern = [ 1, 83.5 ]
            //thickHorizontalLayer.lineDashPhase   = 0.25

            self.layer.addSublayer(thickHorizontalLayer)

            let thinDashesPath = UIBezierPath()
            thinDashesPath.move(to: CGPoint(x: 0, y: thinYCoord)) //esquerda
            thinDashesPath.addLine(to: CGPoint(x: 340, y: thinYCoord)) //direita

            //thinHorizontalLayer.frame            = frame
            thinHorizontalLayer.path             = thinDashesPath.cgPath
            thinHorizontalLayer.strokeColor      = UIColor.black.cgColor
            thinHorizontalLayer.lineWidth        = 15.0
            thinHorizontalLayer.fillColor        = UIColor.clear.cgColor
            thinHorizontalLayer.lineDashPattern  = [ 0.5, 7.95]
            //thinHorizontalLayer.lineDashPhase    = 0.25

            self.layer.addSublayer(thinHorizontalLayer)

您需要将公共代码放入由initframe:和initcoder:调用的例程中:

我还建议为@IBInspectable类型声明一个显式类型,否则您将无法在IB中调整它们

就我个人而言,与其硬编码路径宽度,不如在布局改变时更新它。此外,如果要使这些属性@IBDesignable,那么如果路径发生更改,您确实需要更新它们

@IBDesignable
class AnalogView: UIView {

    fileprivate let thickHorizontalLayer = CAShapeLayer()
    fileprivate let thinHorizontalLayer = CAShapeLayer()

    @IBInspectable var thickYCoord: CGFloat = 50.0 { didSet { updatePaths() } }
    @IBInspectable var thinYCoord:  CGFloat = 52.5 { didSet { updatePaths() } }

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

        configure()
    }

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

        configure()
    }

    private func configure() {
        layer.addSublayer(thickHorizontalLayer)
        layer.addSublayer(thinHorizontalLayer)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        updatePaths()
    }

    private func updatePaths() {
        let thickDashesPath = UIBezierPath()
        thickDashesPath.move(to: CGPoint(x: bounds.origin.x, y: thickYCoord)) //left
        thickDashesPath.addLine(to: CGPoint(x: bounds.origin.x + bounds.size.width, y: thickYCoord)) //right

        thickHorizontalLayer.path            = thickDashesPath.cgPath
        thickHorizontalLayer.strokeColor     = UIColor.black.cgColor //dashes color
        thickHorizontalLayer.lineWidth       = 20
        thickHorizontalLayer.lineDashPattern = [1.0, NSNumber(value: Double(bounds.size.width - 1) / 4 - 1.0) ]

        let thinDashesPath = UIBezierPath()
        thinDashesPath.move(to: CGPoint(x: bounds.origin.x, y: thinYCoord)) //esquerda
        thinDashesPath.addLine(to: CGPoint(x: bounds.origin.x + bounds.size.width, y: thinYCoord)) //direita

        thinHorizontalLayer.path             = thinDashesPath.cgPath
        thinHorizontalLayer.strokeColor      = UIColor.black.cgColor
        thinHorizontalLayer.lineWidth        = 15.0
        thinHorizontalLayer.fillColor        = UIColor.clear.cgColor
        thinHorizontalLayer.lineDashPattern  = [0.5, NSNumber(value: Double(bounds.size.width - 1) / 40 - 0.5)]
    }
}

你可能想调整短划线以跨越宽度,我不确定你是想要一个一致的比例还是它跨越宽度。但希望这能说明这个想法。

你的initcoder:实现在哪里?谢谢你的建议@Rob。我需要摆脱使用硬编码的值。
@IBDesignable
class AnalogView: UIView {

    fileprivate let thickHorizontalLayer = CAShapeLayer()
    fileprivate let thinHorizontalLayer = CAShapeLayer()

    @IBInspectable var thickYCoord: CGFloat = 50.0 { didSet { updatePaths() } }
    @IBInspectable var thinYCoord:  CGFloat = 52.5 { didSet { updatePaths() } }

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

        configure()
    }

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

        configure()
    }

    private func configure() {
        layer.addSublayer(thickHorizontalLayer)
        layer.addSublayer(thinHorizontalLayer)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        updatePaths()
    }

    private func updatePaths() {
        let thickDashesPath = UIBezierPath()
        thickDashesPath.move(to: CGPoint(x: bounds.origin.x, y: thickYCoord)) //left
        thickDashesPath.addLine(to: CGPoint(x: bounds.origin.x + bounds.size.width, y: thickYCoord)) //right

        thickHorizontalLayer.path            = thickDashesPath.cgPath
        thickHorizontalLayer.strokeColor     = UIColor.black.cgColor //dashes color
        thickHorizontalLayer.lineWidth       = 20
        thickHorizontalLayer.lineDashPattern = [1.0, NSNumber(value: Double(bounds.size.width - 1) / 4 - 1.0) ]

        let thinDashesPath = UIBezierPath()
        thinDashesPath.move(to: CGPoint(x: bounds.origin.x, y: thinYCoord)) //esquerda
        thinDashesPath.addLine(to: CGPoint(x: bounds.origin.x + bounds.size.width, y: thinYCoord)) //direita

        thinHorizontalLayer.path             = thinDashesPath.cgPath
        thinHorizontalLayer.strokeColor      = UIColor.black.cgColor
        thinHorizontalLayer.lineWidth        = 15.0
        thinHorizontalLayer.fillColor        = UIColor.clear.cgColor
        thinHorizontalLayer.lineDashPattern  = [0.5, NSNumber(value: Double(bounds.size.width - 1) / 40 - 0.5)]
    }
}